Azure Runbook(工作流)输出结果到 Table

Azure Runbook (workflow) output result to Table

我正在重新创建 Azure Runbook 以配合逻辑应用程序功能。长话短说,我希望逻辑应用启动 Runbook,从 Runbook 中获取结果并将它们用于逻辑应用中的后续步骤。

最初我想获取启动一些 VM 时的 JSON 输出,结果我得到的输出是这样的:

{
    "MyVM2":  true
}

{
    "MyVM1":  true
}

然后我打算解析要在 Runbook 中使用的 JSON,但很快意识到我不会得到一致数量的结果(可能是 2 个 VM,或 20 个)我找到了 Parse JSON 模式仅解析我设置的模式(2,在我的例子中,所以会遗漏更多)。

现在我想我可以将我的输出放入 table,然后使用它来允许逻辑应用程序查看 table 内部以提取我的 VM 名称和成功结果。所以,这是我一直在破坏的 Runbook:

workflow ShutDownStartByTagasdf
{
        Param(
        [Parameter(Mandatory=$true)]
        [String]
        $TagName,
        [Parameter(Mandatory=$true)]
        [String]
        $TagValue,
        [Parameter(Mandatory=$true)]
        [Boolean]
        $Shutdown
        )

    $connectionName = "AzureRunAsConnection";

    try
    {
        # Get the connection "AzureRunAsConnection "
        $servicePrincipalConnection=Get-AutomationConnection -Name $connectionName        

        # Logging in to Azure
        $null = Add-AzureRmAccount `
            -ServicePrincipal `
            -TenantId $servicePrincipalConnection.TenantId `
            -ApplicationId $servicePrincipalConnection.ApplicationId `
            -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
    }
    catch {

        if (!$servicePrincipalConnection)
        {
            $ErrorMessage = "Connection $connectionName not found."
            throw $ErrorMessage
        } else{
            Write-Error -Message $_.Exception
            throw $_.Exception
        }
    }


    $vms = Find-AzureRmResource -TagName $TagName -TagValue $TagValue | where {$_.ResourceType -like "Microsoft.Compute/virtualMachines"}

    Foreach -Parallel ($vm in $vms){
        if($Shutdown){
            $StopRtn = Stop-AzureRmVm -Name $vm.Name -ResourceGroupName $vm.ResourceGroupName -Force;
            $objOut = [PSCustomObject]@{
                VM = $vm.Name
                Success = $StartRtn.IsSuccessStatusCode
            }
        }
        else {
            $StartRtn = Start-AzureRmVm -Name $vm.Name -ResourceGroupName $vm.ResourceGroupName;
            $objOut = New-Object psobject -Property @{
                VM = $vm.Name
                Success = $StartRtn.IsSuccessStatusCode
                }
        }
        $outPut = InlineScript {
            $Using:objOut | Format-Table Vm,Success
        }
    }
}

忽略 $objOut = [PSCustomObject]@{ 部分,那只是我的 JSON 乱七八糟的历史,我暂时把它留在那里,因为我没有使用 $Shutdwon = $True,只是$False,也就是else {

之后的最后一部分

这位:

else {
    $StartRtn = Start-AzureRmVm -Name $vm.Name -ResourceGroupName $vm.ResourceGroupName;
    $objOut = New-Object psobject -Property @{
        VM = $vm.Name
        Success = $StartRtn.IsSuccessStatusCode
        }
}
$outPut = InlineScript {
    $Using:objOut | Format-Table Vm,Success
}

}

我正在尝试按照此处已描述的内容实施一些操作:Create Table with Variables in PowerShell

但是 Azure Runbook 控制台没有输出,但它确实启动了 VM。

一个非常冗长的解释,但有谁知道我如何输出到工作流(Runbook)中的格式化 Table,这将在一个 table 中产生我的所有结果?

我最终使用数组来捕获信息:

workflow ShutDownStartByTagasdf
{
        Param(
        [Parameter(Mandatory=$true)]
        [String]
        $TagName,
        [Parameter(Mandatory=$true)]
        [String]
        $TagValue,
        [Parameter(Mandatory=$true)]
        [Boolean]
        $Shutdown
        )

    $connectionName = "AzureRunAsConnection";

    try
    {
        # Get the connection "AzureRunAsConnection "
        $servicePrincipalConnection=Get-AutomationConnection -Name $connectionName        

        # "Logging in to Azure..."
        $null = Add-AzureRmAccount `
            -ServicePrincipal `
            -TenantId $servicePrincipalConnection.TenantId `
            -ApplicationId $servicePrincipalConnection.ApplicationId `
            -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
    }
    catch {

        if (!$servicePrincipalConnection)
        {
            $ErrorMessage = "Connection $connectionName not found."
            throw $ErrorMessage
        } else{
            Write-Error -Message $_.Exception
            throw $_.Exception
        }
    }

    $result_array = @()

    $vms = Find-AzureRmResource -TagName $TagName -TagValue $TagValue | where {$_.ResourceType -like "Microsoft.Compute/virtualMachines"}

    Foreach -Parallel ($vm in $vms) {
        if($Shutdown){
            # Write-Output "Stopping $($vm.Name)";
            $StopRtn = Stop-AzureRmVm -Name $vm.Name -ResourceGroupName $vm.ResourceGroupName -Force;
            $objOut = New-Object -TypeName psobject -Property @{
                VM = $vm.Name
                Success = $StopRtn.IsSuccessStatusCode
            }
        }
        else {
            # Write-Output "Starting $($vm.Name)"; 
            $StartRtn = Start-AzureRmVm -Name $vm.Name -ResourceGroupName $vm.ResourceGroupName;
            $objOut = New-Object -TypeName psobject -Property @{
                VM = $vm.Name
                Success = $StartRtn.IsSuccessStatusCode
                }
        }

        $workflow:result_array += $objOut
    }

    $result_array | ConvertTo-Json
}

最后一点 $result_array | ConvertTo-Json 让我获得了更好的输出,我希望在 Logic App 中使用它。输出:

[
    {
        "VM":  "MyVM2",
        "Success":  true,
        "PSComputerName":  "localhost",
        "PSShowComputerName":  true,
        "PSSourceJobInstanceId":  "dadd87ad-1de1-432c-92b1-4c501c9a7ce8"
    },
    {
        "VM":  "MyVM1",
        "Success":  true,
        "PSComputerName":  "localhost",
        "PSShowComputerName":  true,
        "PSSourceJobInstanceId":  "dadd87ad-1de1-432c-92b1-4c501c9a7ce8"
    }
]

我不喜欢它是一个对象数组,因为现在我必须弄清楚如何在逻辑应用程序中围绕它 { } 来更好地利用它。