我对这个问题感到非常震惊。我请你回答或给个提示。我的选择不多了。
我通过 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]
输出:
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句