powershellazure-automationazure-runbook

How can I retrieve output from Azure PowerShell child Runbook (7.2) started by 'Start-AutomationRunbook' from within parent Runbook (also 7.2)?


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.


Solution

  • 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 !!!