How to pass WEBHOOKDATA parameter value in Azure automation Runbook

Gaurav Garg 0 Reputation points
2023-09-14T08:37:02.39+00:00


Hi,

Could you please help me out to pass WEBHOOKDATA parameter value in Azure Automation Runbook.

for testing using the below code to display the webhookdata parameter value on testpane

Param

(

  [parameter (Mandatory=$false)]

  [object]$WebhookData

)

Write-Output $WebhookData

Write-Output $WebhookData.WebhookName

Write-Output $WebhookData.RequestHeader

Write-Output $WebhookData.RequestHeader.header1

Write-Output $WebhookData.RequestBody

Please let me know in which format I have to pass this parameter value at runtime in runbook.

Also use this parameter through C# code also.

Regards

GGarg

Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,250 questions
{count} votes

2 answers

Sort by: Most helpful
  1. tbgangav-MSFT 10,416 Reputation points
    2023-09-20T07:59:30.4066667+00:00

    Hi @Gaurav Garg ,

    As explained here, for the WebhookData parameter in the UI for testing in Test pane of the runbook, you must pass the data in JSON object with carriage returns and newline characters, so it matches the format that is passed in from a webhook.

    For example, pass the following sample JSON object in the UI for the WebhookData parameter for a sample runbook shared below.

    {"WebhookName":"mywebhook","RequestBody":"[\r\n {\r\n "ResourceGroup": "rg01",\r\n "Name": "vm01"\r\n },\r\n {\r\n "ResourceGroup": "rg02",\r\n "Name": "vm02"\r\n }\r\n]"}
    
    param
    (
        [Parameter (Mandatory=$true)]
        [object] $WebhookData
    )
    
    # Logic to allow for testing in Test pane
    if (-Not $WebhookData.RequestBody) { 
        $WebhookData = (ConvertFrom-Json -InputObject $WebhookData)
        Write-Output "test1 $WebhookData"
        Write-Output "test2"
        Write-Output $WebhookData.RequestBody
        Write-Output "test3"
        Write-Output $WebhookData.RequestHeader
        Write-Output "test4"
        Write-Output $WebhookData.RequestHeader.header1
    }
    
    

    For illustration, check below screenshots of runbook execution and output. Note that in this example RequestHeader and header1 under RequestHeader is not showing any output as the input JSON object doesn't have those related details. So, based on the use case you might have to provide input JSON object in the required format with carriage returns and newline characters, so it matches the format that is passed in from a webhook. User's image

    User's image

    In the real-time scenario, request body of the incoming POST request of webhook parameter can keep any data formatting such as string, JSON, XML, or form-encoded. The runbook must be written to work with the data format that is expected.

    0 comments No comments

  2. 90973623 0 Reputation points
    2024-10-18T14:02:44.8066667+00:00

    UPDATED

    Moral of the story - you need to check if input is an object (how it will appear coming from webhook) or a string (how it will appear coming from the UI). If it's a string, use ConvertFrom-Json to get an object.

    Passing the below JSON to the UI will work.

    {"WebhookName":"mywebhook","RequestBody":[{"ResourceGroup": "rg01","Name": "vm01"},{"ResourceGroup": "rg02","Name": "vm02"}]}
    
    

    User's image


    Tested with your script and JSON input. It does not function the same.

    Input JSON

    User's image

    Output

    User's image

    Script copied from your post

    User's image

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.