azurepowershellwebhooksrunbook

Can't set parameters in Azure Automation Account webhook in Powershell


I have a powershell script runbook in Azure Automation account (tested with Powershell 5.1 and 7.2).

This script needs Parameters:

#set arguments
param(
    [string]$Origin,
    [string]$title,
    [string]$Description
)

I can execute the script directly in Azure and provide my parameters. But when I run a script to trigger the webhook of the runbook and provide the parameters, then no parameters are parsed in the script. I do this by the following powershell script:

# Define the parameters
$params = @{
    "ORIGIN" = "value1"
    "TITLE" = "value2"
    "DESCRIPTION" = "value3"
}

# Convert the parameters to a JSON string
$jsonParams = $params | ConvertTo-Json

try {
    # Invoke the webhook
    Invoke-RestMethod -Method Post -Uri $webhookUrl -Body $jsonParams -ContentType "application/json"
} catch {
    Write-Host "Error: $_"
}

The webhook is triggered with the following input in the runbook logs:

{"WebhookName":"CreateItem","RequestBody":"{\r\n \"TITLE\": \"value2\",\r\n \"DESCRIPTION\": \"value3\",\r\n \"ORIGIN\": \"value1\"\r\n}","RequestHeader":{"Connection":"Keep-Alive","Host":"0117e8aa-9b60-4cb6-b844-2f558f86d135.webhook.we.azure-automation.net","User-Agent":"Mozilla/5.0","x-ms-request-id":"cdb71f35-c396-4f05-801a-b4cc155c146b"}}

When I log the parameters in the script like follows:

Write-Output "Using $Origin as my the origin"
Write-Output "Using $title as the title"
Write-Output "Using the following description: $Description"

Then this is the output:

Using as my the origin

Using as the title

Using the following description:

I am out of ideas what to do, please help :)


Solution

  • I figured it out, apparently you need to read the sended data from the $webhookData.

    The runbook should include:

    param  
    (  
        [Parameter(Mandatory = $false)]  
        [object] $WebhookData  
    )  
    
    $Inputs = ConvertFrom-Json $webhookdata.RequestBody 
    
    $origin = $Inputs.origin
    $title = $Inputs.title
    $Description = $Inputs.Description
    

    This will read the webhookdata and converts to requestbody to readable content.