0
votes

I'm a beginner with Powershell who's trying to send a PUT request to Microsoft Azure to create an Application Insights log query. I'm able to get this working in Postman, but not in a powershell script. This is the error that I'm getting when running the script:

Invoke-RestMethod : {"code":"Unexpected character encountered while parsing value: S. Path '', line 0, position 0.","message":"Unexpected character encountered while parsing value: S. Path '', line 0, position 0.","innererror":{"diagnosticcontext":"f2843c54-dad7-49b5-92ab-e1dadd40e145","time":"2020-07-24T19:59:45.7979293Z"}}
At C:\Users\thophan\source\Update-AiLogQueries.ps1:58 char:9
+ Invoke-RestMethod -Method Put -Uri $uri -Header $header -Body ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

From looking at the Microsoft documentation, it looks like it's supposed to be able to take in a hashtable for the body, and I'm quite certain my syntax looks exactly the same as the one from the exameple, so I'm not sure what's going on. Below is an excerpt from my script:

$scope = "shared"
$header = @{
    "Authorization" = "Bearer $token"
}
$uri = "https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/$rgName/providers/microsoft.insights/components/$aiName/analyticsItems/item?api-version=2020-02-02-preview"
$difference = Compare-Object -ReferenceObject $localQueryList.value -DifferenceObject $response
if ($difference -ne $null)
{
    $difference.ForEach(
    {
        $body = @{
            Scope = $scope
            Type = "query"
            Name = $_.InputObject.Name
            Content = $_.InputObject.Content
        }

        Invoke-RestMethod -Method Put -Uri $uri -Header $header -Body $body
    })
}

UPDATE: Here is the documentation that I was looking at, as requested. https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-restmethod?view=powershell-5.1

1
Can you provide the documentation link you followed?AdminOfThings
@AdminOfThings I've updated the post to include the link as you requested.Thomas Phan
Apologies. I was hoping there was API documentation you were following rather than the PowerShell command.AdminOfThings
@AdminOfThings If that's the case, I did not follow any API documentation. I got the URI from using the F12 Dev Tool, and then I tested the URI with the body as shown above in Postman, which works fine. So now I'm just trying to translate that over to using Powershell scripting.Thomas Phan
The contents of $difference is a major unknown here because not only are you accessing a reference object but properties of that reference object. Do you know if the API required JSON body in the PUT? If so, a lot of API calls error because they should have included a JSON body string --> -Body ($body | convertTo-json) -ContentType 'application/json'.AdminOfThings

1 Answers

1
votes

The API requires that the body be a JSON string. You can do a simple conversion (using ConvertTo-Json) in your Invoke-RestMethod command and set the content type accordingly.

Invoke-RestMethod -Method Put -Uri $uri -Header $header -Body ($body | ConvertTo-Json) -ContentType 'application/json'