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

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

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

运行手册如下所示

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 的作业部分,查看作业的输入和输出。输入如下所示:

{"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?我在网上找不到任何实际有效的例子。

您应该像这样在 inlinescript {} 块中使用 $using: 作用域:

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("Variables in InlineScript" 部分)。