gpt4 book ai didi

powershell - Azure Automation Runbook 无法将 webhook 数据解析为 JSON 对象

转载 作者:行者123 更新时间:2023-12-02 20:05:05 25 4
gpt4 key购买 nike

我被这个问题严重打击了。我请求你回答或给予提示。我的选项用完了。

我正在通过 WebHook 在高 CPU 使用率时调用 azure runbook。我的问题是运行手册中的数据没有被正确解码。例如,下面一行没有打印任何东西。

 Write-Output $WebHookData.RequestHeader

如果我尝试像这样将数据显式转换为 JSON

*$WebhookData = ConvertFrom-Json $WebhookData*

那么就是抛错了。

ConvertFrom-Json : Invalid JSON primitive: . At line:6 char:31 + $WebhookData = $WebhookData | ConvertFrom-Json

顺便说一句,我正在尝试使用 Azure 库中提供的运行手册{使用 Azure 自动化垂直扩展 Azure 资源管理器 VM}

我的 Webhook 是从 VM 上创建的警报调用的。

一个非常奇怪的观察:

工作 WebHood 示例(在示例中找到){"WebhookName":"test1","RequestBody":"[\r\n {\r\n\"Message\":\"测试消息\"\r\n }\r\n****]****"

不工作(从 VM 调用 runbook 时发送的数据):

{"WebhookName":"test2","RequestBody":"{\"schemaId\":\"AzureMonitorMetricAlert\"}}

谢谢

最佳答案

我遇到了同样的错误。根据我的测试,似乎在执行 Runbook 的“测试”时,Webhook 数据以纯文本形式接收,但在远程调用时,它已经格式化为 JSON。这是我涵盖这两种情况的解决方案,到目前为止一直运行良好......

Param (
[object] $WebhookData
)
# Structure Webhook Input Data
If ($WebhookData.WebhookName) {
$WebhookName = $WebhookData.WebhookName
$WebhookHeaders = $WebhookData.RequestHeader
$WebhookBody = $WebhookData.RequestBody
} ElseIf ($WebhookData) {
$WebhookJSON = ConvertFrom-Json -InputObject $WebhookData
$WebhookName = $WebhookJSON.WebhookName
$WebhookHeaders = $WebhookJSON.RequestHeader
$WebhookBody = $WebhookJSON.RequestBody
} Else {
Write-Error -Message 'Runbook was not started from Webhook' -ErrorAction stop
}

关于powershell - Azure Automation Runbook 无法将 webhook 数据解析为 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54976909/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com