Passing Custom Objects between Azure runbooks

1k views Asked by At

I need to call a runbook from another runbook and get a custom object as output in azure automation . It works fine If the called runbook returns int or string but returning Custom Objects could not be done.A simple example for the called runbook is

     workflow CalledRunbook 
     {
        [OutputType([Object])]
        $obj1=@{"key1"="value1"}
        $obj1
     } 

Now this runbook is called from the CallingRunbook and I need to print this obj1

   workflow CallingRunbook 
   {
      #After doing proper authentication 
      $job = Start-AzureAutomationRunbook -Name "CalledRunbook" -AutomationAccountName $AutomationAccountName 

      $doLoop = $true
      while($doLoop) {
            Start-Sleep -s 5

            $job = Get-AzureAutomationJob -Id $job.Id -AutomationAccountName $AutomationAccountName

            $doLoop = (($job.Status -notmatch "Completed") -and ($job.Status -notmatch "Failed") -and ($job.Status -notmatch "Suspended")  -and ($job.Status -notmatch "Stopped"))
        }

        $jobout = Get-AzureAutomationJobOutput `
                                -Id $job.Id `
                                -AutomationAccountName $AutomationAccountName `
                                -Stream Output
        if ($jobout) {
              Write-Output $jobout
        }
    }

The output is empty. If I return a string That works perfectly fine. How to make it work with custom objects ?

1

There are 1 answers

0
Joe On BEST ANSWER

Each output record of a runbook job in Azure Automation is always stored as a string, regardless of type. If the object being outputted is not a string, it is serialized as a string. It would appear this object does not serialize to a string correctly, so Azure Automation is not storing its string version as job output.

To workaround this, my recommendation would be to serialize / deserialize the object yourself between the two runbooks. Use ConvertTo-Json on the object in CalledRunbook, and output the result of that. This will output the object as a JSON string. Then, in CallingRunbook, call ConvertFrom-Json on the output of CalledRunbook, to get the original object back.