简体   繁体   English

Powershell v5.1 Invoke-RestMethod 错误解析体

[英]Powershell v5.1 Invoke-RestMethod Error Parsing Body

I'm a beginner with Powershell who's trying to send a PUT request to Microsoft Azure to create an Application Insights log query.我是 Powershell 的初学者,他正在尝试向 Microsoft Azure 发送 PUT 请求以创建 Application Insights 日志查询。 I'm able to get this working in Postman, but not in a powershell script.我可以在 Postman 中使用它,但不能在 powershell 脚本中使用。 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"}} Invoke-RestMethod: {"code":"解析值时遇到意外字符: S. Path '', line 0, position 0.","message":"解析值时遇到意外字符: 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在 C:\Users\thophan\source\Update-AiLogQueries.ps1:58 char:9
+ Invoke-RestMethod -Method Put -Uri $uri -Header $header -Body... + Invoke-RestMethod -Method Put -Uri $uri -Header $header -Body...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~
+ CategoryInfo: InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException + CategoryInfo: InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId: WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand + 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.从查看 Microsoft 文档来看,它看起来应该能够为正文获取一个哈希表,而且我很确定我的语法看起来与示例中的语法完全相同,所以我不确定是什么继续。 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 https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-restmethod?view=powershell-5.1

The API requires that the body be a JSON string. API 要求主体是 JSON 字符串。 You can do a simple conversion (using ConvertTo-Json ) in your Invoke-RestMethod command and set the content type accordingly.您可以在Invoke-RestMethod命令中进行简单的转换(使用ConvertTo-Json )并相应地设置内容类型。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM