Powershell v5.1调用RestMethod解析正文时出错

Powershell v5.1调用RestMethod解析正文时出错,powershell,rest,azure-powershell,powershell-5.0,windows-scripting,Powershell,Rest,Azure Powershell,Powershell 5.0,Windows Scripting,我是Powershell的初学者,正在尝试向Microsoft Azure发送PUT请求以创建Application Insights日志查询。我可以在Postman中实现这一点,但不能在powershell脚本中实现。这是我在运行脚本时遇到的错误: 调用RestMethod:{“代码”:“分析值时遇到意外字符:S.Path“”,第0行,位置0.”,“消息”:“分析值时遇到意外字符:S.Path“”,第0行,位置0.”,“内部错误”:{“diagnosticcontext”:“f2843c54-

我是Powershell的初学者,正在尝试向Microsoft Azure发送PUT请求以创建Application Insights日志查询。我可以在Postman中实现这一点,但不能在powershell脚本中实现。这是我在运行脚本时遇到的错误:

调用RestMethod:{“代码”:“分析值时遇到意外字符:S.Path“”,第0行,位置0.”,“消息”:“分析值时遇到意外字符:S.Path“”,第0行,位置0.”,“内部错误”:{“diagnosticcontext”:“f2843c54-dad7-49b5-92ab-e1dadd40e145”,“时间”:“2020-07-24T19:59:45.79293Z”}
在C:\Users\thophan\source\Update ailogquerys.ps1:58 char:9
+调用RestMethod-methodput-Uri$Uri-Header$Header-Body…
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+类别信息:无效操作:(System.Net.HttpWebRequest:HttpWebRequest)[调用RestMethod],WebException
+FullyQualifiedErrorId:WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeSMethodCommand

从微软的文档来看,它看起来应该能够接受一个散列表作为主体,我很确定我的语法看起来与示例中的语法完全相同,所以我不确定发生了什么。以下是我的脚本摘录:

$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
    })
}
更新:这是我根据要求查看的文档。
API要求主体是JSON字符串。您可以在命令中进行简单转换(使用),并相应地设置内容类型

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

你能提供你关注的文档链接吗?@AdminOfThings我已经按照你的要求更新了帖子,添加了链接。抱歉。我希望您遵循的是API文档,而不是PowerShell命令。@如果是这样的话,我没有遵循任何API文档。我使用F12开发工具获得了URI,然后我用body测试了URI,如上面的Postman中所示,效果很好。因此,现在我正试图将其转换为使用Powershell脚本。这里的
$difference
的内容是一个主要未知内容,因为您不仅访问引用对象,而且还访问该引用对象的属性。您知道API是否需要PUT中的JSON体吗?如果是这样,很多API调用都会出错,因为它们应该包含一个JSON正文字符串-->
-body($body | convertTo JSON)-ContentType“application/JSON”