Title says it all, I guess.

I am trying to modularize my runbooks and all I want to do is start child RBs using the internal Automation module 'Start-AutomationRunbook' AND get back the result.

This is the sample code I am testing with:

Parent RB:

$result = Start-AutomationRunbook -Name ChildRB
Write-Output $result

Child RB:

$hello = 'Hello world!'
Write-Output $hello

However, instead of storing 'Hello world!' in $result the Job ID is sent to the console.

2

There are 2 best solutions below

0
On BEST ANSWER

I posted the same question here: https://learn.microsoft.com/en-us/answers/questions/1417541/how-can-i-retrieve-output-from-azure-powershell-ch

And I received the following answer, which actually works:

In order to get the Job output from the child runbook, use Get-AzAutomationJobOutput cmdlet from the Az.Automation module. Following is a sample script to start a child runbook, wait for it to complete and then get the output back.

#Using Managed Identity for Authentication - START
# 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
#Using Managed Identity for Authentication -END

$result = Start-AutomationRunbook -Name ChildRB

#wait for the job to complete else you may get empty result which creates confusion
Wait-AutomationJob -Id $result

#get the output from child runbook.
$out = Get-AzAutomationJobOutput $result -ResourceGroupName "new-autoAcc" -AutomationAccountName "autoacc" -Stream "Any"

write-output $out.Summary

Note that "get-AzAutomationJobOutput" is not an internal automation cmdlet (hence the name contains "Az"). The only internal cmdlets as available are here - https://learn.microsoft.com/en-us/azure/automation/shared-resources/modules#internal-cmdlets

Kudos to AnuragSingh-MSFT !!!

8
On

I'm assuming that these work like Jobs in PowerShell. Jobs return their ID instead of their results because their results might not be ready yet, because the job is still running.

You can either add the -Wait parameter to Start-AutomationRunbook, this will cause the script to wait at this point until the results are ready. Then you need to get the results from the job object. Simply:

$Job = Start-AutomationRunbook -Name ChildRB -Wait

# Get the job results by returned ID
$Result = Receive-Job -Job $Job

# Print your output
Write-Output $Result

Or you can keep your code as-is and add a separate section for collecting results once jobs are ready. The advantage of this method is that you can run jobs in parallel, where as in the first they run one at a time.

This would look something like this:

# Run a single runbook and gather its results
$Job = Start-AutomationRunbook -Name ChildRB

# Get the job object and wait on it.
Get-Job -Name "ChildRB" | Wait-Job

# Get the job results by returned ID
$Result = Receive-Job -Job $Job

# Print your output
Write-Output $Result

Of course, the above still only runs and waits for one command and you're not making use of your parallel possibilities, in which case you could do something like this, although note that processing outputs from parallel jobs gets progressively more complex:

# Imagine we have a list of RunBooks all with names like RunBook-1, RunBook-2 etc.
Foreach ($RunBookNumber in 1..10) {
  # Kick off all 10 of our runbooks in parallel
  Start-AutomationRunbook -Name Runbook-$RunBookNumber
}

# Wait for all of our jobs to be complete, we can supply a wildcard here.
$Jobs = Get-Job -Name "Runbook-*" | Wait-Job

# Loop through all the results from our jobs
Foreach ($JobResult in $Jobs) {
  # Receive-Job is what actually gets the result data, which is an array of every output returns in the order it was output.
  $Results = Receive-Job -Job $JobResult

  # Iterate through the array and print them all to the console.
  Foreach ($Result in $Results) {
    Write-Output $Result
  }
}

I've made a fair few assumptions here on how Start-AutomationRunbook works, but if it's anything like Start-AzAutomationRunbook then I think this should work.