Search code examples
powershellparameterswebhooksazure-automation

Azure Automation Account: How to Pass Webhook data to a runbook?


I have an Azure Runbook in an Azure Automation account, which I would like to trigger with a webhook which contains some parameters.

The runbook looks like this

workflow do-something
{

    param
    (
        [object]$WebhookData
    )
    inlinescript { 

        if ($WebhookData -ne $null) {
            $WebhookName =  $WebhookData.WebhookName
            $WebhookBody =  $WebhookData.RequestBody

            $webhookBodyObject = $WebhookBody | ConvertFrom-JSON
            $customerEmail = $webhookBodyObject.customerEmail
            $customerName = $webhookBodyObject.customerName
            $dataLocation = $webhookBodyObject.dataLocation

        } else {
            "The WebhookData is totally and completely  null"
            exit (0)
        }
        $webhookjson = $WebhookData | ConvertTo-JSON
        "The webhookdata is $webhookjson"
        "The webhook name is $WebhookName"
        "The customer email is $customerEmail"
        "The body s $WebhookBody"
    }
}

I then saved it and published it, and then got a webhook for it. As per instructions, i wrote a little Powershell script to trigger the webhook:

#Not the real URI, but similar in structure
$uri = "https://s10events.azure-automation.net/webhooks?token=Qt%xyxyxyxyxyxyxyxyxyxyxyxy%ababababab%3d"
$headers = @{"From"="babu@bhatt.com";"Date"="05/28/2015 15:47:00"}

$params = @{"customerName"="Jay Godse"; "customerEmail"="jaygodse@exmple.com"; "dataLocation"="Canada"}
$body = ConvertTo-Json -InputObject $params

#$response = Invoke-RestMethod -Method Post -Uri $uri -Headers $headers -Body $body
$webresp = Invoke-WebRequest -Method Post -Uri $uri -Headers $headers -Body $body -Verbose

When I invoked the request, i got a 202 response code which suggested that the request was successfully queued.

Then I went to the Jobs section in the Runbook, and and looked at the input and output of the job. The input looked like this:

{"WebhookName":"test1","RequestBody":"{\r\n \"customerEmail\": \"jaygodse@exmple.com\",\r\n \"customerName\": \"Jay Godse\",\r\n \"dataLocation\": \"Canada\"\r\n}","RequestHeader":{"Connection":"Keep-Alive","Date":"Thu, 28 May 2015 19:47:00 GMT","From":"babu@bhatt.com","Host":"s10events.azure-automation.net","User-Agent":"Mozilla/5.0","x-ms-request-id":"d8995f98-1344-4822-af69-ababababababa"}}

The output looked like this:

The WebhookData is totally and completely  null

What do I have to do to pass data successfully from the webhook to the my Azure Automation runbook? I couldn't find any examples on the web which actually worked.


Solution

  • You should use $using: scope inside the inlinescript {} block like that:

    workflow do-something
    {
    
        param
        (
            [object]$WebhookData
        )
        inlinescript {
    
            if ($using:WebhookData -ne $null) {
                $WebhookName =  $using:WebhookData.WebhookName
                $WebhookBody =  $using:WebhookData.RequestBody
    
                $webhookBodyObject = $WebhookBody | ConvertFrom-JSON
                $customerEmail = $webhookBodyObject.customerEmail
                $customerName = $webhookBodyObject.customerName
                $dataLocation = $webhookBodyObject.dataLocation
    
            } else {
                "The WebhookData is totally and completely  null"
                exit (0)
            }
            $webhookjson = $using:WebhookData | ConvertTo-JSON
            "The webhookdata is $webhookjson"
            "The webhook name is $WebhookName"
            "The customer email is $customerEmail"
            "The body s $WebhookBody"
        }
    }
    

    See the explanation here: https://technet.microsoft.com/en-us/library/jj574197(v=ws.11).aspx ("Variables in InlineScript" section).