Powershell input parameters with runbook

Raj D 591 Reputation points
2022-08-16T19:21:53.917+00:00

Greetings...

I'm exploring different options to execute an azure automation runbook written in powershell that has a few input parameters. I have looked at the microsoft documentation but still not clear on how to implement this. Now, how would I pass the input parameters along with the webhook uri to execute the underlying azure automation runbook.

Powershell:

$uri = "https://webhook.us.azure-automation.net/webhooks?token=abc"  
Invoke-WebRequest -Method Post -Uri $uri   
  
#Input parameter  
  
Param(  
 [string]$user,  
 [string]$password  
)  

Thank you in advance.

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

Accepted answer
  1. Andreas Baumgarten 111.6K Reputation points MVP
    2022-08-16T22:13:03.43+00:00

    Hi @Raj D ,

    let's start with a simple Automation Runbook with a simple PowerShell script.

    Create a new Automation Runbook and add the following PowerShell script:

    param  
    (  
        [Parameter(Mandatory = $false)]  
        [object] $WebhookData  
    )  
    $Inputs = ConvertFrom-Json $webhookdata.RequestBody  
    Write-Output "Name of VM = $($Inputs.vmName)"  
    Write-Output "Name of ResourceGroup = $($Inputs.rgName)"  
    

    231755-image.png

    Create a webhook for the Runbook and save the URL

    231802-image.png

    Run the following PowerShell script on your computer to start the Azure Automation Runbook with input parameters :

    $webhookURI = '<your-webhook-URI>'  
    $params  = @{  
    vmName ="TestVM1";  
    rgName ="test1-rg"  
    }  
    $body = ConvertTo-Json -InputObject $params  
    $response = Invoke-WebRequest -Method Post -Uri $webhookURI -Body $body -UseBasicParsing  
    $response  
    

    The output of the Runbook should look like this:

    231766-image.png

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    4 people found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Maxim Sergeev 6,571 Reputation points Microsoft Employee
    2022-08-16T19:38:44.383+00:00

    Hi there,

    To receive data from the client, the runbook supports a single parameter called WebhookData. This parameter defines an object containing data that the client includes in a POST request.

    In this case you need to use and parse a standard variable $WebhookData

    Param(  
         $WebhookData  
     )  
      
     # Payload comes in as a json object so convert the body into PowerShell friendly object.  
    $RequestBody = ConvertFrom-Json $WebhookData.RequestBody  
      
    # Get the results from the table object  
    $Result =  $RequestBody.data.SearchResult.tables  
    

    https://learn.microsoft.com/en-us/azure/automation/automation-webhooks?tabs=portal#parameters-used-when-the-webhook-starts-a-runbook

    1 person found this answer helpful.

  2. Andreas Baumgarten 111.6K Reputation points MVP
    2022-08-16T20:08:22.08+00:00

    Hi @Raj D ,

    Here is a different approach: https://learn.microsoft.com/en-us/azure/automation/runbook-input-parameters

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten


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.