API request to Azure DevOps from PowerShell

Piotr Rogala
2 min readApr 13, 2020

We often face the challenge of integrating many tools. If we use PowerShell to automate our infrastructure or to build simple scripts, we may need to use the API request call using PowerShell. The following example demonstrates the use of sending an API request to Azure DevOps to add an agent pool.

API reference for Azure DevOps you can find here: https://docs.microsoft.com/en-us/rest/api/azure/devops/distributedtask/pools/add?view=azure-devops-rest-5.1

The following sample script can be expanded for other operations using the appropriate Uri for the needed action from the link above.

At the beginning we define variables:

$urlvsts = 'https://dev.azure.com/PROJECT-NAME'
$token = 'YOUR-PAT-TOKEN'
$pool = 'YOUR-POOL-NAME'
$encodedPat = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes( ":$token"))
$body = "{name:`"$pool`", autoProvision: `"true`"}"

Command Invoke-WebRequest performs sending a request giving a predefined variable and contains Uri or the place where the API request will set a new pool in Azure DevOps.

Script:

Invoke-WebRequest `
-Method POST `
-Uri "$urlvsts/_apis/distributedtask/pools?api-version=5.0- preview.1" `
-UseBasicParsing `
-Headers @{Authorization = "Basic $encodedPat"} `
-Body $body `
-ContentType "application/json"

Screens:

Script:

Output:

Azure DevOps:

We can now easily automate operations in Azure DevOps without launching the portal. Unfortunately, the API reference for Azure DevOps does not allow everything we can do in the portal, but it will certainly help in achieving the goal. If you have a compelling case of using the mentioned API or you think where you can use it, I invite you to comment.

You can also check my script used in the creation agent pool for Azure DevOps here: https://github.com/RogalaPiotr/JustCloudPublic/blob/master/simple-vm-with-installation-vsts-agent/vstsagent.ps1

--

--