I am trying to capture ARM template Output and pass it into Azure Powershell script which is running next. For this , i am doing the below steps and all these are done with Azure DevOps.
- Declaring outputs in ARM template.
"outputs": {
"adminUsername": {
"type": "string",
"value": "[parameters('adminUsername')]"
},
"virtualMachineName": {
"value": "[parameters('virtualMachineName')]",
"type": "string"
}
}
- Passing ARM template Output into 1st Azure Powershell script to declare each output value as a pipeline variable. 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'
- I am trying to use the above created pipeline variables "virtualMachineName" in my 2nd Azure powershell script as an argument.
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. It seems $virutalMachineName is not available as a pipeline variable.
Can somebody please help ? My questions are 1.Whether pipeline variables are set and available globally in the pipeline from successfully executed task? 2.If so, how to use the newly created pipeline variable in my 2nd Azure Powershell script ?
Disk-encryption -virtualMachineName '$(virtualMachineName)'
, can you try simplyDisk-encryption -virtualMachineName $virtualMachineName
? – mclaytonDisk-encryption -virtualMachineName $virtualMachineName | ~~~~~~~~~~~~~~~~~~~ | Cannot bind argument to parameter 'virtualMachineName' because | it is an empty string
– Sivakumar Kayaroganam