1

I'm using an Azure API > https://learn.microsoft.com/en-us/rest/api/azure/devops/git/pull%20request%20query/get?view=azure-devops-rest-5.1

I'm using this code:

$body = @"
{
    "queries": [{
        "items": [
            "59c1c31397b266116ff6d735e5638ef5d1b598a0"
        ],
        "type": "commit"
    }]
}
"@ 

$x = Invoke-RestMethod -Uri $someLink -Headers @{Authorization = $pat } -Body $body -Method Post -ContentType 'application/json'
Write-Host $x
Write-Host $x | ConvertFrom-Json
Write-Host $x.results | ConvertFrom-Json

I've removed the link for security reasons.

When I run the code I get the following in my console:

2020-01-20T16:17:55.8600905Z @{queries=System.Object[]; results=System.Object[]}

2020-01-20T16:17:55.8637026Z @{queries=System.Object[]; results=System.Object[]}

2020-01-20T16:17:55.8674193Z

I'm not sure if the queries and results objects are empty or that I need some other way of reading them.

Community
  • 1
  • 1
Peter Boomsma
  • 8,851
  • 16
  • 93
  • 185

1 Answers1

3
  • Write-Host writes to the host (which is typically the console / terminal), bypassing PowerShell's success output stream, which is why a command such as Write-Host $x | ConvertFrom-Json is pointless, because ConvertFrom-Json will receive no input.

  • Write-Host writes simple .ToString() stringifications of non-string input objects to the host, which means that you don't get PowerShell's rich, human-friendly output formatting that implicit output or - rarely needed - explicit Write-Output calls result in when ultimately printing to the host by default, due to the output neither getting captured in a variable or getting redirected with >).

  • Invoke-RestMethod automatically parses the JSON output it receives into PowerShell custom objects ([pscustomobject] instances, so there is also also no conceptual reason to pipe its property values to ConvertFrom-Json - unless the API truly returns nested JSON text as string values in the JSON it returns.


If you want to visually inspect the results, simply output them directly:

$x  # output visualization of the whole object
$x.results  # just the .results property

The above outputs to the success output stream, which means that an outside caller of your code would receive these values as part of the code's output.

If the resulting table- or list-like formatting (depending on the number of properties) doesn't tell you enough, you can convert the objects back to JSON, by piping to ConvertTo-Json. Note that the latter's default serialization depth is limited to 2, so you may have to pass a higher -Depth value to see the object in full - see this post.

$x | ConvertTo-Json  # use -Depth <n>, if needed.
$x.results | ConvertTo-Json

If you really want to just print the values as information for the user, without becoming part of the output, use Out-Host rather than Write-Host, because Out-Host does apply the rich formatting:

$x | Out-Host # print visualization of the whole object directly to the host
$x.results | Out-Host  # just the .results property

# Ditto via ConvertTo-Json
$x | ConvertTo-Json | Out-Host
...
mklement0
  • 382,024
  • 64
  • 607
  • 775