How to Queue an Azure DevOps yaml Pipeline with Parameters from PowerShell

Long enough title? It might be longer than the actual article content.

If you need to queue a yaml pipeline in Azure DevOps from a script, you may be tempted to use the az cli and the devops extension. The extension is useful, but has some bugs/gaps I would recommend using it in combination with the Azure DevOps REST API.

This DOES NOT work:

az pipelines run --organization "https://dev.azure.com/organization/" `
    --project "project" `
    --name "Pipeline" `
    --open `
    --variables foo=bar --debug

But this rest request does:

POST https://dev.azure.com/organization/project/_apis/pipelines/25/runs?api-version=6.0-preview.1
Content-Type: application/json
X-VSS-ForceMsaPassThrough: true
Authorization: Basic <PAT>
{
    "templateParameters": {"foo": "bar"}
}

Use this PowerShell to create your rest request:

$AzureDevOpsOrganization = "myorg"
$AzureDevOpsProject = "myproject"
$PAT = "<Azure DevOps Personal Access Token>"
$devopsProjectUrl = "https://dev.azure.com/$AzureDevOpsOrganization/$AzureDevOpsProject"
$headers = @{
Authorization = "Basic $PAT";
'X-VSS-ForceMsaPassThrough' = $true
}
$body = @{
templateParameters = @{foo = "bar"}
}
$json = $body | ConvertTo-Json
$pipelineRunUrl = "$devopsProjectUrl/_apis/pipelines/$pipelineId/runs?api-version=6.0-preview.1"
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$pipelineRunResponse = Invoke-RestMethod -Uri $pipelineRunUrl `
-Method POST -Headers $headers -ContentType 'application/json' `
-Body $json -Verbose

2 Replies to “How to Queue an Azure DevOps yaml Pipeline with Parameters from PowerShell”

Leave a Reply

Your email address will not be published. Required fields are marked *