3

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

Vijay
  • 168
  • 1
  • 2
  • 11

3 Answers3

4

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
}
NickD
  • 86
  • 5
1

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

Joy Wang
  • 39,905
  • 3
  • 30
  • 54
  • Hi Joy, Thanks for the response. I have confusion... You are trying to send webhook from PowerShell...But my runbook is throwing an error when webhook is called from "Alert upon some certain event".. For example whenever CPU utilization is above 90% and aleert will get triggered which will call our runbook as action . In that case, this runbook will throw error.. which I mentioned.. – Vijay Mar 04 '19 at 15:57
  • @Vijay Could you test with my runbook script? I don't think the issue is related to the way of sending the webhook. – Joy Wang Mar 05 '19 at 00:57
  • Just tried it. And yes it is working... A big thank you :) – Vijay Mar 05 '19 at 13:58
  • just curious to know, what was the problem with my runbook? – Vijay Mar 05 '19 at 13:58
  • @Vijay Not sure, maybe missed something. – Joy Wang Mar 05 '19 at 15:11
0

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
Owais Ahmad
  • 329
  • 1
  • 4
  • 8