1

I am trying to publish a comment to the PR from the sample output file as the comment body. I had difficulties in preserving the human readable format in the body as it was printing it as one line. Now I think I made it to work but still the Invoke-WebRequest is failing with "innerException":null,"message":"Value cannot be null".

I can see that content is formatted correctly in $body but still null message is the error. What am I doing wrong here?

$JsonContent = Get-Content .\temp.output -Raw
$bodyFromJson = @{"comments"=@{"parrentCommentId"=0;"comment"=$JsonContent;"commentType"="1"};"properties"=@{"Microsoft.TeamFoundation.Discussion.SupportsMarkdown"=@{"type"="System.Int32";"value"="1"};"status"="1"}}
$body = $bodyFromJson | ConvertTo-Json -Depth 100
$uri = "https://dev.azure.com/uri"
$headers = @{
"Content-Type" = "application/json"
"Authorization" = "Bearer $env:SYSTEM_ACCESSTOKEN"
}
Invoke-WebRequest -Uri $Uri -Method Post -Headers $headers -Body $body

Now I am getting the below error

Error Message

temp.output content is below

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
~ update in-place

Update will perform the following actions:

# Resource ABC will be updated in-place
~ resource “resource_name” {
endpoint_name = “ABC”
endpoint_type = “xyz”
priority = 2
~ weight = 1 -> 10

custom_header {
name = “host”
value = “host.test.com”
}
}

Plan: 0 to add, 1 to change, 0 to destroy.
Matt
  • 3,658
  • 3
  • 14
  • 27
Robin
  • 11
  • 2
  • 1
    Welcome to StackOverflow. Can you please check so that the comment doesnt end up as null in your $body. – Daniel Björk Aug 07 '20 at 08:04
  • Could you please edit the question to include the error text? This would help others to locate your question using a search engine, as well as come up with a useful answer. – Nikolay Shebanov Aug 07 '20 at 13:44
  • @Nick - Updated the question to include the error. – Robin Aug 07 '20 at 16:00
  • @Dan - yes, $body is not null. – Robin Aug 07 '20 at 16:00
  • Check te content of $JsonContent – Daniel Björk Aug 07 '20 at 16:33
  • @Dan, $jsoncontent, $bodyfromjson, $body everything showing the correct and expected values. – Robin Aug 07 '20 at 16:58
  • The `temp.output` uses curly 'smaty-quotes' (`“` and `”`). Are you sure these are allowed? What is you change these into straight quotes? – Theo Aug 07 '20 at 19:27
  • That didn't do any magic either. Still the same error. – Robin Aug 09 '20 at 00:37
  • Honestly, I'm surprised you're not running into [this issue](https://github.com/PowerShell/PowerShell/issues/6388). The workaround mentioned by markekraus in that thread may help. Also, [check this](https://stackoverflow.com/a/36368402/11609403). PowerShell may be encountering a problem with the web server's response. Adding `-Verbose` to your `Invoke-WebRequest` line may shed more insights. – leeharvey1 Aug 09 '20 at 03:04
  • What if you change one-line JSON format? Kindly check this blog PowerShell Script to call REST API--https://www.opentechguides.com/how-to/article/azure/201/devops-rest-powershell.html If it help in your situation. – PatrickLu-MSFT Aug 31 '20 at 09:56

0 Answers0