Start A Child Runbook From Azure Automation Hybrid Worker on the Same Hybrid Worker Group

1 minute read

Today I was writing a PowerShell runbook (let’s call it Runbook A) that’s designed to run on on-prem hybrid workers. At the end of Runbook A, I needed to kick off another runbook (let’s call it Runbook B) that must run on the same Hybrid Worker group. Because I don’t want to hardcode the Hybrid Worker group name in the script (or using an Automation variable), I wrote a very simple function that returns the Hybrid Worker configuration (including the Hybrid Worker group name) from registry if runs on a Hybrid Worker.

To use it, simply place the function shown below in the parent runbook (Runbook A in this case), and call this function to retrieve the Hybrid Worker configuration.

Function:

Function Get-HybridWorkerConfig
{
  $RegKeyPath = "HKLM:\SOFTWARE\Microsoft\HybridRunbookWorker"
  If (Test-Path $RegKeyPath)
  {
    Get-ItemProperty $RegKeyPath
  } else {
    $null
  }
}

Code Sample:

#Get the Hybrid Worker group name
$HybridWorkerGroup = (Get-HybridWorkerConfig).RunbookWorkerGroup

#Preparing input parameters for Runbook B
$params = @{
  "Parameter1" = 'Value1';
  "Parameter2" = 'Value2';
  "Parameter3" = 'Value3'
}

#Log on the Azure using Login-AzureRmAccount cmdlet
Login-AzureRmAccount -Credential $AzureCred -SubscriptionName $SubscriptionName

#Start Runbook B using Start-AzureRmAutomationRunbook cmdlet
Start-AzureRmAutomationRunbook -ResourceGroupName $ResourceGroupName -AutomationAccountName $AutomationAccountName -Name "RunbookB" -Parameters $params -RunOn $HybridWorkerGroup

Note: The Get-HybridWorkerConfig function would return $null value if the computer is not a Hybrid Worker.

Leave a comment