在PS

时间:2019-06-08 16:31:41

标签: azure azure-devops

我正在尝试在Azure Devops Pull Request线程中创建注释:

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

# Variables
$organization = "movieseat"
$project = "pokedex"
$repositoryId = "Pokedex"
$pullRequestId = "97"
$threadId = "283" 

$pat = "Bearer $env:System_AccessToken"

$body = @"
{
    "content"="Finished building feature branch"
    "commentType"="text";
}
"@ 

$postURL = "https://dev.azure.com/$organization/$project/_apis/git/repositories/$repositoryId/pullRequests/$pullRequestId/threads/$threadId/comments?api-version=5.0"
$prComment = Invoke-RestMethod -Uri $postURL -Headers @{Authorization = $pat} -Body $body -Method Post -ContentType 'application/json'

Write-Output $prComment

但是在发布步骤中,我得到了:

Invoke-RestMethod : {"$id":"1","innerException":null,"message":"Value cannot be null.\r\nParameter name: 
comment","typeName":"System.ArgumentNullException, 
mscorlib","typeKey":"ArgumentNullException","errorCode":0,"eventId":0}
At D:\a\r1\a\_Pokedex master\PokeDexArtifact\release\commentURL.ps1:49 char:14
+ ... prComment = Invoke-RestMethod -Uri $postURL -Headers @{Authorization  ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
##[error]PowerShell exited with code '1'.
##[section]Finishing: PowerShell Script

我找不到与此错误有关的任何信息。

2 个答案:

答案 0 :(得分:0)

我不确定100%,但是我认为这是实际的错误:

  

message”:“值不能为空。\ r \ n参数名称:comment”,

$body = @"
    {
        "content": "http://google.com",
        "commentType": "text";
    }
"@ 

像这样运行代码。注意内容值后面的,。尽管我删除了commentType值,因为它也可以不使用它。

//编辑。我已经更新了代码块,在内容行中将=替换为:

答案 1 :(得分:0)

{
    "content"="Finished building feature branch"
    "commentType"="text";
}

那不是有效的JSON。在PowerShell中处理创建JSON字符串的最佳方法是在关联数组上使用ConvertTo-Json

$body = @{
 content = 'Finished building feature branch'
 commentType = 'text'
} | ConvertTo-Json -Depth 10