The previous sample scripts on this blog has been about how to use Powershell through the REST API on a on-premise TFS. I’ve got some question about how to connect to Visualstudio Online (VSTS).
All the samples can be modified to connect to VSTS.
First you need create a PAT (Personal Access Token) See MS guide on this Use PAT
Then you need to create a Base64 access token based on the username and the PAT
$AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $UserName,$PAT)))
Then pass this access token as a header to your request.
here we get all build definitions
(Invoke-RestMethod -Uri ($URL + '/_apis/build/definitions?api-version=2.0') -ContentType "application/json" -Method GET -Headers @{Authorization=("Basic {0}" -f $AuthInfo)}).value
Note!!! the PAT and the access token should be considered as passwords, so they should be handled in a proper and secure way.
Full script
$VSTSProject = "TFSDemo" $UserName="TestUser" #PAT should be threated as a password!!! $PAT="jf7d6eejs8fffwssn65hiqahpf74hokpocdsfdsfkle882kjs9ya" $URL = "https://MYVSTS.visualstudio.com/DefaultCollection/$($VSTSProject)" #Create Base64 AccessToken $AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $UserName,$PAT))) #Get list of builds (Invoke-RestMethod -Uri ($URL + '/_apis/build/definitions?api-version=2.0') -ContentType "application/json" -Method GET -Headers @{Authorization=("Basic {0}" -f $AuthInfo)}).value