Skip to content

Instantly share code, notes, and snippets.

@kkamegawa
Created December 8, 2017 13:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kkamegawa/fed4b31082f53dac70f2d0f069651e40 to your computer and use it in GitHub Desktop.
Save kkamegawa/fed4b31082f53dac70f2d0f069651e40 to your computer and use it in GitHub Desktop.
Invoke VSTS Build definition by Build Name.
[string]$vstsAccount = "<your VSTS account>"
[string]$projectName = "<your VSTS Project Name>"
[string]$user = "<user authentication Name>"
[string]$token = "<Personal Access Token>"
[string]$builddefname = "Build Definition Name"
# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
$uri = "https://$($vstsAccount).visualstudio.com/$($projectName)/_apis/build/builds?api-version=2.0"
$result = Invoke-RestMethod -Uri $uri -Method Get -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
$counter = 0
if($result.count -eq 0){
write-host "no builds"
return
}
$builddefarray = @{}
for($counter = 0;$counter -lt $result.count;$counter++){
if($builddefarray.ContainsKey([string]$result.value[$counter].definition.name) -eq $false){
$builddefarray += @{[string]$result.value.definition[$counter].name = $result.value[$counter].definition.id}
}
}
$body = @{
definition= @{
id= $builddefarray[$builddefname]
}
}
$result = Invoke-RestMethod -Uri $uri -Method Post -ContentType "application/json" -body (ConvertTo-Json $body) -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment