I have an Azure Automation runbook that kicks off a child runbook for each subscription.
foreach ($sub in $subscriptions)
{
$ChildRunbookInputParams = @{"SubscriptionId"="$($sub.SubscriptionId)"}
$job = Start-AzureRmAutomationRunbook `
-Name $ChildRunbookName `
-Parameters $ChildRunbookInputParams `
-ResourceGroupName $AutomationAccountResourceGroup `
-AutomationAccountName $AutomationAccountName
}
The parent and child runbooks both use the same run-as account.
The problem I'm running into is that the runbooks seem to be sharing contexts, even though they are running as independent jobs.
The parent runbook does a Set-AzureRmContext
to the subscription containing the automation account so that it can Start-AzureRmAutomationRunbook
. The child runbook does a Set-AzureRmContext
to the subscription ID that it is passed so it can process resources in that subscription.
After the first child runbook starts, subsequent Start-AzureRmAutomationRunbook
calls in the parent runbook fail because it can no longer find the specified runbook. And as additional child runbooks are started (the parent runbook gets several child jobs kicked off before the first one changes subscriptions) the previously started child jobs start processing resources for the last subscription.
Is this the expected behavior? Working with multiple subscriptions seems like a common requirement; what's the recommended pattern for this?