在 Azure Runbook 之间传递自定义对象

Passing Custom Objects between Azure runbooks

我需要从另一个 runbook 调用一个 runbook 并获取一个自定义对象作为 azure automation 中的输出。如果被调用的 runbook returns int 或 string 但 returning 自定义对象不能 done.A 则它工作正常被调用的 runbook 的简单示例是

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

现在从 CallingRunbook 调用此 Runbook,我需要打印此 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
        }
    }

输出为空。如果我 return 一个 string That 工作得很好。如何使其与自定义对象一起使用?

Azure 自动化中 Runbook 作业的每条输出记录始终存储为字符串,无论类型如何。如果输出的对象不是字符串,则将其序列化为字符串。该对象似乎没有正确序列化为字符串,因此 Azure Automation 没有将其字符串版本存储为作业输出。

要解决此问题,我的建议是在两个运行手册之间自行序列化/反序列化对象。在 CalledRunbook 中的对象上使用 ConvertTo-Json,并输出结果。这会将对象输出为 JSON 字符串。然后,在 CallingRunbook 中,对 CalledRunbook 的输出调用 ConvertFrom-Json,以取回原始对象。