简体   繁体   English

将 ARM 模板输出作为管道变量传递给 Azure powershell 任务时出现问题

[英]Issue while passing ARM template outputs as a pipeline variable to Azure powershell task

I am trying to capture ARM template Output and pass it into Azure Powershell script which is running next.我正在尝试捕获 ARM 模板输出并将其传递到接下来运行的 Azure Powershell 脚本中。 For this , i am doing the below steps and all these are done with Azure DevOps.为此,我正在执行以下步骤,所有这些都是使用 Azure DevOps 完成的。

  1. Declaring outputs in ARM template.在 ARM 模板中声明输出。
 "outputs": {
    "adminUsername": {
        "type": "string",
        "value": "[parameters('adminUsername')]"
    },
     "virtualMachineName": {
         "value": "[parameters('virtualMachineName')]",
         "type": "string"
        }
}

  1. Passing ARM template Output into 1st Azure Powershell script to declare each output value as a pipeline variable.将 ARM 模板输出传递到第一个 Azure Powershell 脚本,以将每个输出值声明为管道变量。 So this can be used in my next task.所以这可以在我的下一个任务中使用。
    function Convert-ArmOutputToPsObject {
      param (
        [Parameter(Mandatory=$true)]
        [string]
        $ArmOutputString
      )
      if ($PSBoundParameters['Verbose']) {
        Write-Host "Arm output json is:"
        Write-Host $ArmOutputString
      }
      $armOutputObj = $ArmOutputString | ConvertFrom-Json
      $armOutputObj.PSObject.Properties | ForEach-Object {
          $type = ($_.value.type).ToLower()
          $keyname = "ArmOutputs.$($_.name)"
          $value = $_.value.value

          if ($type -eq "securestring") {
              Write-Host "##vso[task.setvariable variable=$keyname;issecret=true]$value"
              Write-Host "Added Azure DevOps secret variable '$keyname' ('$type')"
          } elseif ($type -eq "string") {
              Write-Host "##vso[task.setvariable variable=$keyname]$value"
              Write-Host "Set environment variable to ($env:$keyname)"
              Get-ChildItem Env:
              Write-Host "ARMoutput Id ($keyname)
          } else {
              Throw "Type '$type' is not supported for '$keyname'"
          }
      }
    }
    Convert-ArmOutputToPsObject -ArmOutputString '$(ArmOutputs)' -Verbose

Output of the above script is succesful: From this i could see the pipeline variables are set and available上面脚本的输出是成功的:从这里我可以看到管道变量已设置并可用

Arm output json is:
{"adminUsername":{"type":"String","value":"azureuser"},"virtualMachineName":{"type":"String","value":"vm-test"}}
**##[debug]Processed: ##vso[task.setvariable variable=adminUsername;isOutput=true]azureuser**
Added Azure DevOps variable 'adminUsername' ('string') with value 'azureuser'
##[debug]Processed: ##vso[task.setvariable variable=virtualMachineName;isOutput=true]vm-test
Added Azure DevOps variable 'virtualMachineName' ('string') with value 'vm-test'
  1. I am trying to use the above created pipeline variables "virtualMachineName" in my 2nd Azure powershell script as an argument.我试图在我的第二个 Azure PowerShell 脚本中使用上面创建的管道变量“virtualMachineName”作为参数。
function Disk-encryption {
                                param (
                                [Parameter(Mandatory=$true)]
                                [string]
                                $virtualMachineName)
write-host $virtualMachineName
}
Disk-encryption -virtualMachineName '$(virtualMachineName)'

Execution of this script throws below error执行此脚本会引发以下错误

Error:错误:

Line |
   2 | Disk-encryption -virtualMachineName $(virtualMachineName)
     |                                   ~~~~~~~~~~~~~~~~~~
     | The term 'virtualMachineName' is not recognized as the name of
     | a cmdlet, function, script file, or operable program. Check
     | the spelling of the name, or if a path was included, verify
     | that the path is correct and try again.

Even just tried to print only 'write-host $virtualMachineName' but getting same error.甚至只是尝试只打印 'write-host $virtualMachineName' 但得到同样的错误。 It seems $virutalMachineName is not available as a pipeline variable.似乎 $virutalMachineName 不能用作管道变量。

Can somebody please help ?有人可以帮忙吗? My questions are 1.Whether pipeline variables are set and available globally in the pipeline from successfully executed task?我的问题是 1. 管道变量是否在成功执行的任务的管道中设置和全局可用? 2.If so, how to use the newly created pipeline variable in my 2nd Azure Powershell script ? 2.如果是这样,如何在我的第二个 Azure Powershell 脚本中使用新创建的管道变量?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM