[英]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 完成的。
"outputs": {
"adminUsername": {
"type": "string",
"value": "[parameters('adminUsername')]"
},
"virtualMachineName": {
"value": "[parameters('virtualMachineName')]",
"type": "string"
}
}
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'
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 脚本中使用新创建的管道变量?
You should use output variables instead of normal variables (see https://docs.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch#use-output-variables-from-tasks ).您应该使用输出变量而不是普通变量(请参阅https://docs.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch#use-output-variables -来自任务)。
Also, take a look at https://adamtheautomator.com/arm-output-variables-in-azure-pipelines-powershell/ .另外,请查看https://adamtheautomator.com/arm-output-variables-in-azure-pipelines-powershell/ 。 It explains perfectly how to do this.
它完美地解释了如何做到这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.