Azure Runbook Webhook,带有来自httpClient的参数

时间:2018-05-03 23:12:16

标签: json powershell runbook

查看了一些如何使用参数执行webhook的示例,但似乎无法建立我所缺少的连接。任何关于我做错的建议都会受到赞赏。

请考虑: 我的Powershell Runbook

[CmdletBinding()]
Param([object]$WebhookData) #this parameter name needs to be called 
WebHookData otherwise the webhook does not work as expected.

$VerbosePreference = 'continue'
Write-Output "hello"
    "in the inline"
    if($WebhookData -ne $null) 
        {
            "using webhookdata"
            $WebhookName =  $WebhookData.WebhookName
            $WebhookBody =  $WebhookData.RequestBody
            $webhookBodyObject = $WebhookBody | ConvertFrom-JSON


line 15            'The parameter created was ' $webhookBodyObject.strYear

我的httpclient帖子请求看起来像这样(warning..its vb)

dim WebHookData as new StringContent("{'strYear'='2018'}",Encoding.UTF8,"application/json")

        Dim resp as Task(Of HttpResponseMessage)
        resp = _client.PostAsync(webhook,WebHookData)
        status = resp.Result.Content.ReadAsStringAsync().Result
        if(status.Contains("JobId"))
            status = "Scheduled!"
        End If

我的Webhook数据正在发布到我的webhook中。

{"WebhookName":"myimportjob","RequestBody":"{'strYear'='2018'}","RequestHeader":{"Connection":"Keep-Alive","Expect":"100-continue","Host":"xxx.azure-automation.net","x-ms-request-id":"xxx"}}

我收到此错误

  

在行:15 char:42        +'创建的参数是'$ webhookBodyObject.strYear        + ~~~~~~~~~~~~~~~~~~表达式中出现意外的标记'$ webhookBodyObject'或       言。

1 个答案:

答案 0 :(得分:0)

我发现魔鬼在细节中。 首先我有

$webhookBodyObject = $WebhookBody | ConvertFrom-JSON

不一样
$webhookBodyObject = $WebhookBody | ConvertFrom-Json <---this is the correct syntax

另一个是我发送的json有这样的单引号&#39; {&#34; key&#34;:&#34; value&#34;}&#39;

出于某种原因,尽管它通过了常规的PowerShell,但Runbook并不喜欢它。它希望它的Json像这样{&#34; key&#34;:&#34; value&#34;}。我从来没有测试过复杂的物体,所以我无法说出来。