Run azure automation runbook using management api

Raj D 581 Reputation points


I'm trying to explore different options to execute azure automation runbook using management api and job create. I'm working on passing parameters from job execution to the powershell code in the azure automation runbook. The code below is trying to pass the parameters VmName, ResourceGroup from the job execution code to azure automation runbook code. And run the execution on a hybrid worker group.


# Runbook Powershell  
# Ensures you do not inherit an AzContext in your runbook  
Disable-AzContextAutosave -Scope Process  
# Connect to Azure with system-assigned managed identity  
$AzureContext = (Connect-AzAccount -Identity).context  
# set and store context  
$AzureContext = Set-AzContext -SubscriptionName $AzureContext.Subscription -DefaultProfile $AzureContext  
# Convert object to actual JSON  
$json = $json | ConvertFrom-Json  
# Use the values from the JSON object as the parameters for your command  
Start-AzVM -Name $json.VMName -ResourceGroupName $json.ResourceGroup -DefaultProfile $AzureContext  
# Job Execution Powershell  
$token = "abc123"  
$header = @{  
 "Authorization" = "Bearer " + $token  
 "Content-Type" = "application/json"  
$sid = "sid"  
$rg = "rgname"  
$azautoaccount = "azautoaccountname"  
$job = "jobname"  
$runbook = "test"  
$Uri = "$sid/resourceGroups/$rg/providers/Microsoft.Automation/automationAccounts/$azautoaccount/jobs/$job" + "?api-version=2019-06-01"  
$json = @"  
        "VmName" : "TestVM",  
        "ResourceGroup" : "$rg"  
Invoke-WebRequest -Method PUT -Uri $Uri -UseBasicParsing -ContentType "application/json" -Headers $header -Body $json  
Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,160 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,431 questions
{count} votes

Accepted answer
  1. AnuragSingh-MSFT 20,991 Reputation points

    Hi @Raj D ,

    Thank you for the clarification. I now understand that you are trying to invoke a Runbook with parameters using webhook and it needs to run on "Hybrid Worker Group". Based on the sample provided in the questions, below are the changes required to get it to work:

    1. Since you are passing parameters when invoking the runbook, the parameter name in the runbook script has to be WebhookData. The invocation engine matches the parameter passed to webhook URI to the named parameter WebhookData. In your case, you have named it json, hence the binding cannot happen, causing the error. Please see this link for details.

    2. The parameters passed are available to runbook using $WebhookData.RequestBody. Please find below the sample runbook script with these changes

          [object]$WebhookData  #the pamater for biding passed parameter  
     # Convert object to actual JSON. Note that "WebhookData.RequestBody" is being used to convertFrom-Json  
    $json = $WebhookData.RequestBody | ConvertFrom-Json  
    write-output "Passed Parameters"  #Output to test that param was passed successfully.  
    write-output $json.VMName  
    write-output $json.ResourceGroup  
    $comp = $env:COMPUTERNAME  #this is to test the computer where it ran. It the output is "CLIENT" - then it ran in Azure sandbox  
    write-output "This RB ran on: $comp"  

    3. To ensure that runbook job runs on Hybrid runbook worker, it will have to be set in WebHook Parmaeters, as shown below:


    Please let me know if you have any questions.

    Please 'Accept as answer' if it helped so that it can help others in the community looking for help on similar topics.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful