Home > Mobile >  How to set/pass runtime variable to powershell task in azure pipeline yaml
How to set/pass runtime variable to powershell task in azure pipeline yaml

Time:05-21

I have the below powershell task in my azure yaml pipeline, however the variable is not being replaced in the getLRId in the $releaseCommitIDApiUrl.Path. It just print the variable AS-IS when script executes and it failed with error. Same problem with the original variable $getLatestReleaseId being used in the Path where it has the value assigned at runtime.

        Write-Host "`$getLatestReleaseId = $getLatestReleaseId"   

        Write-Host "##vso[task.setvariable variable=getLRId;]$($getLatestReleaseId)"

        # Get release Commit ID API endpoint URL
        $releaseCommitIDApiUrl = New-Object System.UriBuilder -ArgumentList 'https://vsrm.dev.azure.com'
        $releaseCommitIDApiUrl.Path = '${{ parameters.organizationName }}/${{ parameters.projectName }}/_apis/release/releases/$(getLRId)'
        $releaseCommitIDApiUrl.Query = 'api-version=6.0'

$getLatestCommitIdResponse = Invoke-RestMethod -Uri $releaseCommitIDApiUrl.Uri.AbsoluteUri -Headers $header -Method GET

Below is the result getting while executing the pipeline. 558 is the values assigned to the variable, but not replaced in URL.


$getLatestReleaseId = 558
##[debug]Processed: ##vso[task.setvariable variable=getLRId;]558
$releaseCommitIDApiUrl = https://vsrm.dev.azure.com:443/<<<maskedvalue>>>/_apis/release/releases/$(getLRId)?api-version=6.0
Invoke-RestMethod: <agent path>/598c95cb-d092-4272-ae79-20d595fc229e.ps1:63
Line |
  63 |  … dResponse = Invoke-RestMethod -Uri $releaseCommitIDApiUrl.Uri.Absolut …
     |                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | {"$id":"1","innerException":null,"message":"VS402853: The
     | parameter with name releaseId should be of type Int32, but the
     | specified value is not convertible to
     | Int32.","typeName":"Microsoft.VisualStudio.Services.ReleaseManagement.Data.Exceptions.InvalidRequestException, Microsoft.VisualStudio.Services.ReleaseManagement2.Data","typeKey":"InvalidRequestException","errorCode":0,"eventId":3000}

CodePudding user response:

The getLRId variable is inside the string, that's why it does not get to be evaluated. try this:

$releaseCommitIDApiUrl.Path = '${{ parameters.organizationName }}/${{ parameters.projectName }}/_apis/release/releases/' $getLRId
  • Related