Skip to content

Instantly share code, notes, and snippets.

@davidroberts63
Last active March 6, 2020 14:28
Show Gist options
  • Save davidroberts63/ef6693d0776b7896d023b842a4a39347 to your computer and use it in GitHub Desktop.
Save davidroberts63/ef6693d0776b7896d023b842a4a39347 to your computer and use it in GitHub Desktop.
AzPi-Build-Canceler
param(
[String]
$ProjectName,
[String]
$AccountName,
[PSCredential]
$TokenCredential
)
Import-Module VSTeam -Version 6.4.4
Set-VSTeamAccount -Account $AccountName -SecurePersonalAccessToken $TokenCredential.GetNetworkCredential().Password
$project = Get-VSTeamProject -Name $ProjectName
$buildsToCancel = (Get-VSTeamBuild -ProjectName $project.Name -StatusFilter notStarted) +
(Get-VSTeamBuild -ProjectName $project.Name -StatusFilter InProgress) +
(Get-VSTeamBuild -ProjectName $project.Name -StatusFilter Postponed)
$buildsToCancel | ForEach-Object {
Invoke-VSTeamRequest -Url ($_._links.self.href + '?api-version=6.0-preview.5') -method patch -body '{"status":4}' -ContentType 'application/json' -verbose
# Last minute change: if the above doesn't work, use the below line.
#Invoke-VSTeamRequest -ProjectName $_.projectName -area 'build' -resource 'Builds' -id $_.Id -method patch -body '{"status":4}' -verbose -version '6.0-preview.5' -ContentType 'application/json'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment