skip to Main Content

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":"{rn "TITLE": "value2",rn "DESCRIPTION": "value3",rn "ORIGIN": "value1"rn}","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 🙂

2

Answers


  1. Chosen as BEST ANSWER

    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.


  2. trigger_webhook.ps1

    $Params  = @(
                @{ Origin="Hawaii"},
                @{ title="Seattle"},
                @{ Description="Florida"}
            )
    
    $body = ConvertTo-Json -InputObject $Params
    
    $webhookURI = "https://xxxxxxx.webhook.eus.azure-automation.net/webhooks?token=xxxxxxxxjkJlQ%3d"
    
    $response = Invoke-WebRequest -Method Post -Uri $webhookURI -Body $body -UseBasicParsing
    $response
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search