在here-string标题之后但在行结束之前不允许使用任何字符

时间:2018-05-23 07:39:28

标签: rest powershell azure-devops

我使用它作为我脚本的基础:

https://www.nwcadence.com/blog/vststfs-rest-api-the-basics-and-working-with-builds-and-releases

我的脚本如下

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)}

但是我在上面的博客文章中使用的身体定义上有语法错误。

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

2 个答案:

答案 0 :(得分:2)

错误消息为您提供了线索:"在here-string标题之后但在结束之前不允许使用任何字符"

更改代码,以便here-string开头/结尾标记不会立即跟随/先于任何内容:

$body = @"
{

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

答案 1 :(得分:2)

您可以通过多种方式更改here-string($body变量的值):

选项1:

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

选项2:

$body = @"
{

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

作为boxdog意思。

选项3:

$body = '
{

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