Azure Automation Runbook无法将Webhookdata解析为JSON对象

发布于 2025-01-23 20:07:28 字数 943 浏览 3 评论 0原文

我对这个问题感到非常震惊。我要求您回答或提示。我的选项用完了。

我正在通过Webhook在高CPU利用率上调用Azure Runbook。我的问题是在运行簿数据中没有正确解码。例如,下行没有打印任何东西。

 Write-Output $WebHookData.RequestHeader

如果我尝试将数据转换为JSON,那么在哪里

*$WebhookData = ConvertFrom-Json $WebhookData*

,这是一个投掷错误。

convertfrom-json:无效的JSON原始:。在线:6 char:31 + $ webhookdata = $ webhookdata | convertfrom-json

,我正在尝试使用Azure Gallery上可用的Runbook {垂直扩展具有Azure Automation的Azure Resource Manager VM}

我的Webhook是从VM上创建的Alert中调用的。

一个非常奇怪的观察:

工作网络期示例(在示例中找到){“ webhookname”:“ test1”,“ requestBody”:“ [ \ r \ r \ n {\ r \ r \ n \ n \ sagess \ \ \ \'' :\ \“ test Messages \” \ r \ n} \ r \ n ****] ****”

不起作用(从VM调用运行书发送的数据):

{“ webhookname”:“ test2”,“请求body” “:” { \“ Schemaid \”:\“ AzuremonItormeticerert \” } }

谢谢

I am badly struck by this problem. I request you to answer or give a hint. I am running out of options.

I am calling an azure runbook upon high CPU utilization via a WebHook. My problem is inside runbook data is not getting decoded properly. For example, the below line is not printing anything.

 Write-Output $WebHookData.RequestHeader

Wheras IF i try to explictly convert the data to JSON, like this

*$WebhookData = ConvertFrom-Json $WebhookData*

then it is a throwing error.

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

By the way, I am trying to use the runbook available on Azure gallery {Vertically scale up an Azure Resource Manager VM with Azure Automation}

My Webhook is called from alert created on VM.

A very strange observation:

Working WebHood Example (found in an example) {"WebhookName":"test1","RequestBody":" [\r\n {\r\n \"Message\": \"Test Message\"\r\n }\r\n****]****"

Not Working(the data sent upon calling runbook from VM):

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

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

初吻给了烟 2025-01-30 20:07:28

我遇到了同样的错误。从我的测试中,看来在执行运行书的“测试”时,将接收到纯文本,但是当被调用时,它通过已格式化为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
}

I was getting the same error. From my testing, it appears that when performing a "Test" of the runbook, the Webhook data is received as plain text, but when invoked remotely it comes through already formatted as JSON. Here was my solution to cover both scenarios and so far has been working well...

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
}
内心荒芜 2025-01-30 20:07:28

我尝试使用webhook,脚本注销输出$ webhookdata.requestheader应该正常工作。

而且,如果我使用convertfrom-json $ webhookdata,我可以重现您的问题,不确定为什么会发生。 /azure/automation/automation-webhooks#参数“ rel =“ nofollow noreferrer”> doc ,$ webhookdata也以json格式,如果接受,则可以使用<代码> convertfrom -json -inputObject $ webhookdata.requestbody ,它可以正常工作。

我的Runbook

param
(
    [Parameter (Mandatory = $false)]
    [object] $WebhookData
)

if ($WebhookData) {

    Write-Output $WebhookData.RequestHeader

    $Body = ConvertFrom-Json -InputObject $WebhookData.RequestBody
    Write-Output $Body

} else
    {
        Write-Output "Missing information";
        exit;
    }

我用来发送webhook 的PowerShell脚本:

$uri = "https://s5events.azure-automation.net/webhooks?token=xxxxxxxxxxxx"

$vms  = @(
            @{ Name="vm01";ResourceGroup="vm01"},
            @{ Name="vm02";ResourceGroup="vm02"}
        )
$body = ConvertTo-Json -InputObject $vms
$header = @{ message="StartedbyContoso"}
$response = Invoke-WebRequest -Method Post -Uri $uri -Body $body -Headers $header
$jobid = (ConvertFrom-Json ($response.Content)).jobids[0]

输出

I tried with a webhook, the script Write-Output $WebHookData.RequestHeader should work fine.

And if I use ConvertFrom-Json $WebhookData, I can reproduce your issue, not sure why it occurred, according to the doc, the $WebhookData is also in a JSON format, if it is accepted, you could use ConvertFrom-Json -InputObject $WebhookData.RequestBody, it will work fine.

My runbook:

param
(
    [Parameter (Mandatory = $false)]
    [object] $WebhookData
)

if ($WebhookData) {

    Write-Output $WebhookData.RequestHeader

    $Body = ConvertFrom-Json -InputObject $WebhookData.RequestBody
    Write-Output $Body

} else
    {
        Write-Output "Missing information";
        exit;
    }

The powershell script I used to send a webhook:

$uri = "https://s5events.azure-automation.net/webhooks?token=xxxxxxxxxxxx"

$vms  = @(
            @{ Name="vm01";ResourceGroup="vm01"},
            @{ Name="vm02";ResourceGroup="vm02"}
        )
$body = ConvertTo-Json -InputObject $vms
$header = @{ message="StartedbyContoso"}
$response = Invoke-WebRequest -Method Post -Uri $uri -Body $body -Headers $header
$jobid = (ConvertFrom-Json ($response.Content)).jobids[0]

Output:

enter image description here

谎言 2025-01-30 20:07:28

如果使用带有警报JSON的测试窗格,我也有相同的问题使用以获取Webhookdata

if(-Not $WebhookData.RequestBody){

    $WebhookData = (ConvertFrom-Json -InputObject $WebhookData)
}
$RequestBody = ConvertFrom-JSON -InputObject $WebhookData.RequestBody

I had the same problem use following to get webhookdata if using test pane with Alert json as input

if(-Not $WebhookData.RequestBody){

    $WebhookData = (ConvertFrom-Json -InputObject $WebhookData)
}
$RequestBody = ConvertFrom-JSON -InputObject $WebhookData.RequestBody
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文