0
votes

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.

  1. Declaring outputs in ARM template.
 "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. 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.
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 ?

1
Instead of Disk-encryption -virtualMachineName '$(virtualMachineName)', can you try simply Disk-encryption -virtualMachineName $virtualMachineName?mclayton
Trying without quotes $virtualMachineName got empty value.. Disk-encryption -virtualMachineName $virtualMachineName | ~~~~~~~~~~~~~~~~~~~ | Cannot bind argument to parameter 'virtualMachineName' because | it is an empty stringSivakumar Kayaroganam
have you tried making it not an output variable ? and if you want to keep it as output variable then you need to use it as $(TaskName.OutputVariableName). Also can you confirm that both tasks as in the same job ?Gurpreet

1 Answers