Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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/$( ..
like image 738
Luis Valencia Avatar asked May 23 '18 07:05

Luis Valencia


2 Answers

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
    }
}
"@
like image 128
boxdog Avatar answered Nov 14 '22 01:11

boxdog


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
    }
}
'
like image 26
Marina Liu Avatar answered Nov 14 '22 01:11

Marina Liu