【问题标题】:Azure Runbook (workflow) output result to TableAzure Runbook(工作流)输出结果到表
【发布时间】:2018-08-01 00:06:13
【问题描述】:

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

最初我想获取启动一些虚拟机的 JSON 输出,最终我得到的输出是这样的:

{
    "MyVM2":  true
}

{
    "MyVM1":  true
}

然后我打算解析要在 Runbook 中使用的 JSON,但很快意识到我不会有一致数量的结果(可能是 2 个 VM,或 20 个)我发现 Parse JSON 模式只能解析什么我将架构设置为(在我的情况下为 2,因此会遗漏更多内容)。

现在我想我可以将我的输出放到一个表中,然后使用它来允许逻辑应用程序查看该表的内部以从中提取我的 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。

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

【问题讨论】:

  • 您最大的问题(可能唯一的问题)是 JSON 输出不是有效的 JSON。那是本机输出吗?你能修改那个格式吗?
  • 因为它的外观,我放弃了输出到 JSON 的想法。正如你所说,它不是真正有效的 JSON。所以,我想看看我是否可以将我的结果输出到 1 个 not JSON 的表中,只是普通的 PowerShell JSON,看看我是否可以在逻辑应用程序中使用它。但是,我在上一节中显示的代码实际上并没有将两个结果的表输出创建为 1,它实际上根本没有做任何事情。这是我需要一些帮助的地方,从两个结果创建一个表(忽略 JSON 部分)。
  • 错字“just normal PowerShell JSON”应该是“just normal PowerShell Table”(不要同时打字和走路!)

标签: powershell azure azure-logic-apps azure-runbook


【解决方案1】:

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

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 让我获得了更好的输出,我希望在逻辑应用程序中使用它。输出:

[
    {
        "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"
    }
]

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

【讨论】:

    猜你喜欢
    • 2016-10-23
    • 2019-01-15
    • 1970-01-01
    • 1970-01-01
    • 2012-09-06
    • 2018-01-08
    • 2018-06-19
    • 2021-01-24
    • 2021-06-01
    相关资源
    最近更新 更多