Azure Automation Runbook unable to parse the webhookdata as a JSON object

4.8k Views Asked by At

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

3

There are 3 best solutions below

0
On BEST ANSWER

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
}
5
On

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

0
On

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