我正在尝试捕获ARM模板输出,并将其传递到下一步运行的Azure Powershell脚本中。为此,我正在执行以下步骤,所有这些步骤都使用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
以上脚本的输出成功: 从中我可以看到管道变量已设置并可用
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)'
执行此脚本会引发以下错误
错误:
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.
即使只是尝试仅打印'write-host $ virtualMachineName',但也会出现相同的错误。 似乎$ virutalMachineName不能用作管道变量。
有人可以帮忙吗? 我的问题是 1.是否已成功执行任务,因此在管道中是否设置了管道变量并在全局范围内可用? 2.如果是这样,如何在第二个Azure Powershell脚本中使用新创建的管道变量?
答案 0 :(得分:0)