I am using this a a basis for my script:
https://www.nwcadence.com/blog/vststfs-rest-api-the-basics-and-working-with-builds-and-releases
And my script is as follows
Param(
[string]$vstsAccount = "abc",
[string]$projectName = "abc",
[string]$user = "",
[string]$token = "xyz"
)
# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
$verb = "POST"
$body = @"{
"definition": {
"id": 20
}
}"@
$uri = "https://$($vstsAccount).visualstudio.com/DefaultCollection/$($projectName)/_apis/build/builds?api-version=4.1"
$result = Invoke-RestMethod -Uri $uri -Method $verb -ContentType "application/json" -Body (ConvertTo-Json $body) -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
But I have syntax errors on the body definition which I used from the blog post above.
> o characters are allowed after a here-string header but before the end
> of the line. At C:\Users\abc\Documents\vstsqueuebuild.ps1:18 char:17
> + "definition": {
> + ~ Unexpected token ':' in expression or statement. At C:\Users\abc\Documents\vstsqueuebuild.ps1:19 char:14
> + "id": 20
> + ~ Unexpected token ':' in expression or statement. At C:\Users\anc\Documents\vstsqueuebuild.ps1:21 char:1
> + }"@
> + ~ Unexpected token '}' in expression or statement. At C:\Users\abc\Documents\vstsqueuebuild.ps1:24 char:9
> + $uri = "https://$($vstsAccount).visualstudio.com/DefaultCollection/$( ..
The error message is giving you a clue: "No characters are allowed after a here-string header but before the end"
Change the code so that the here-string start/end markers are not immediately followed/preceded by anything:
$body = @"
{
"definition": {
"id": 20
}
}
"@
There are multiple ways you can change the here-string (value for the $body
variable):
$body = @{
definition = @{
id = 20
}
}
$body = @"
{
"definition": {
"id": 20
}
}
"@
As boxdog mentions.
$body = '
{
"definition": {
"id": 20
}
}
'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With