[英]Azure Automation Runbook unable to parse the webhookdata as a JSON object
我對這個問題感到非常震驚。 我請你回答或給個提示。 我的選擇不多了。
我通過 WebHook 在 CPU 利用率高時調用 azure runbook。 我的問題是內部 Runbook 數據未正確解碼。 例如,以下行不打印任何內容。
Write-Output $WebHookData.RequestHeader
如果我嘗試將數據顯式轉換為 JSON,就像這樣
*$WebhookData = ConvertFrom-Json $WebhookData*
那么它是一個拋出錯誤。
ConvertFrom-Json:無效的 JSON 原語:。 在行:6 字符:31 + $WebhookData = $WebhookData | ConvertFrom-Json
順便說一句,我正在嘗試使用 Azure 庫上提供的 Runbook {Vertically scale up an Azure Resource Manager VM with Azure Automation}
我的 Webhook 是從在 VM 上創建的警報調用的。
一個非常奇怪的觀察:
工作 WebHood 示例(在示例中找到) {"WebhookName":"test1","RequestBody":" [ \\r\\n {\\r\\n \\"Message\\": \\"Test Message\\"\\r\\n } \\r\\n****]****"
不工作(從 VM 調用 Runbook 時發送的數據):
{"WebhookName":"test2","RequestBody":" { \\"schemaId\\":\\"AzureMonitorMetricAlert\\" } }
謝謝
我嘗試使用 webhook,腳本Write-Output $WebHookData.RequestHeader
應該可以正常工作。
如果我使用ConvertFrom-Json $WebhookData
,我可以重現你的問題,不知道為什么會發生,根據文檔, $WebhookData
也是 JSON 格式,如果它被接受,你可以使用ConvertFrom-Json -InputObject $WebhookData.RequestBody
,它會正常工作。
我的運行手冊:
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]
輸出:
我遇到了同樣的錯誤。 從我的測試來看,似乎在執行 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
}
如果使用帶有 Alert json 作為輸入的測試窗格,我在使用以下獲取 webhookdata 時遇到了同樣的問題
if(-Not $WebhookData.RequestBody){
$WebhookData = (ConvertFrom-Json -InputObject $WebhookData)
}
$RequestBody = ConvertFrom-JSON -InputObject $WebhookData.RequestBody
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.