Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
488 views
in Technique[技术] by (71.8m points)

PowerShell Hashtable as Azure Devops YAML Pipeline output Variable

Is it possible to output a hashtable from an Azure DevOps YAML Pipeline PowerShell Task, store it to a pipeline variable and then pass the hashtable to a second PowersShell task? The receiving Script is running the 'New-AzResourceGroupDeployment' commandlet with "-TemplateParameterObject", which requires a hashtable as the argument.

I have only been able to pass strings to and from pipeline variables. I believe this proves that my syntax for outputting and receiving the variables between stages/tasks is correct. My understanding is that Azure DevOps pipeline variables store strings and there is no way for the engine to recognize a PowerShell hashtable. Is that correct?

Has anyone been able to pass a hashtable as a pipeline variable?

Here is the code which outputs the hashtable:

$TemplateSecretObject = @{
   keyVaultObject       = $KeyVaultObject
   keyVaultSecretObject = $KeyVaultSecretObject
}
Write-Host "##vso[task.setvariable variable=keyVaultSecretTaskOutput;isOutput=true]$TemplateSecretObject"
question from:https://stackoverflow.com/questions/65853767/powershell-hashtable-as-azure-devops-yaml-pipeline-output-variable

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should be able to serilaize hashtable to a JSON string:

'##vso[task.setvariable variable=keyVaultSecretTaskOutput;isOutput=true]{0}' -f (
  $TemplateSecretObject | ConvertTo-JSON -Depth 100 -Compress
)

And then convert it in your script back to hashtable:

$hashtable = '$(SourceTaskName.keyVaultSecretTaskOutput)' | ConvertFrom-JSON -Depth 100 -AsHashtable


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...