Created
December 8, 2017 13:44
-
-
Save kkamegawa/fed4b31082f53dac70f2d0f069651e40 to your computer and use it in GitHub Desktop.
Invoke VSTS Build definition by Build Name.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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