Skip to content

Instantly share code, notes, and snippets.

@tekkies
Last active January 15, 2021 19:06
Show Gist options
  • Save tekkies/e1d6f545fb5f2426c722ca8a3fd42af5 to your computer and use it in GitHub Desktop.
Save tekkies/e1d6f545fb5f2426c722ca8a3fd42af5 to your computer and use it in GitHub Desktop.
Example Query AzureDevOps API Using a Personal Access Token
$ErrorActionPreference = "Stop"
if($env:AdoOrganisation -eq $null)
{
Write-Error 'Please set $env:AdoOrganisation'
}
if($env:AdoProject -eq $null)
{
Write-Error 'Please set $env:AdoProject'
}
if ($secureApiKey -eq $null) {
Write-Error 'Please set personal access token, e.g. $secureApiKey = Get-Credential -Username "(personal access token)"'
}
$AzureDevOpsAuthenicationHeader = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$(ConvertFrom-SecureString ($secureApiKey.Password) -AsPlaintext)")) }
$jsonRequest = [ordered]@{
searchText = "** MY SEARCH STRING**"
'$skip' = 0
'$top' = 10
# See https://docs.microsoft.com/en-us/rest/api/azure/devops/search/work%20item%20search%20results/fetch%20work%20item%20search%20results?view=azure-devops-rest-6.0#examples
# filters= @{
# 'System.TeamProject' = @(
# "$($env:AdoProject)"
# )
# }
includeFacets = "true"
}
$requestBody = $jsonRequest | ConvertTo-Json -Depth 10
$searchResponse = Invoke-WebRequest -Method POST -ContentType "application/json" -Uri "https://almsearch.dev.azure.com/$($env:AdoOrganisation)/$($env:AdoProject)/_apis/search/workitemsearchresults?api-version=6.0-preview.1" -Headers $AzureDevOpsAuthenicationHeader -Body $requestBody
$searchresults = ConvertFrom-Json $searchResponse.Content
$formattedSearchResults = ConvertTo-Json $searchresults
Write-Output $formattedSearchResults
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment