Luis Valencia
Luis Valencia

Reputation: 34038

No characters are allowed after a here-string header but before the end of the line

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/$( ..

Upvotes: 8

Views: 20280

Answers (2)

Marina Liu
Marina Liu

Reputation: 38136

There are multiple ways you can change the here-string (value for the $body variable):

Option 1:

$body = @{
definition = @{
id = 20    
}
}

Option 2:

$body = @"
{

    "definition": {
         "id": 20
    }
}
"@

As boxdog mentions.

Option 3:

$body = '
{

    "definition": {
         "id": 20
    }
}
'

Upvotes: 7

boxdog
boxdog

Reputation: 8442

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
    }
}
"@

Upvotes: 12

Related Questions