Azure自动化帐户:如何将Webhook数据传递给Runbook?

时间:2017-12-12 23:43:01

标签: powershell parameters webhooks azure-automation

我在Azure自动化帐户中有一个Azure Runbook,我想通过包含一些参数的webhook来触发。

Runbook看起来像这样

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"
    }
}

然后我保存并发布了它,然后得到了一个webhook。根据说明,我写了一个小的Powershell脚本来触发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

当我调用请求时,我得到了202响应代码,表明请求已成功排队。

然后我转到Runbook中的Jobs部分,并查看作业的输入和输出。输入看起来像这样:

{"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 WebhookData is totally and completely  null

如何将数据从webhook成功传递到我的Azure自动化Runbook,我该怎么办?我无法在网上找到任何实际有效的例子。

1 个答案:

答案 0 :(得分:1)

您应该在$using:块中使用inlinescript {}范围,如下所示:

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"
    }
}

请参阅此处的说明:https://technet.microsoft.com/en-us/library/jj574197(v=ws.11).aspx(“InlineScript中的变量”部分)。