0
votes

I'm recreating an Azure Runbook to coincide with a Logic App functionality. Long story short, I want the Logic App to initiate the Runbook, grab the results from the Runbook and use them for the next steps in the Logic App.

Initially I wanted to grab the JSON output of starting some VMs, where I ended up with the output being like this:

{
    "MyVM2":  true
}

{
    "MyVM1":  true
}

I was then going to Parse the JSON to be used in the Runbook, but soon realising that I would not have a consisted number of results (maybe 2 VMs, or 20) I had found the Parse JSON schema to only Parse what I set the schema to be (2, in my case, so anything more would be missed).

Now I figure I could put my output to a table, then use that to allow the Logic App to look inside that table to pull my VM names and success results from. So, here is the Runbook that I've been mutilating:

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
        }
    }
}

Ignore the $objOut = [PSCustomObject]@{ part, that's just history of my JSON messing about, and I'm leaving it there for now because I'm not using the $Shutdwon = $True, only $False, which is the very last part after else {

this bit:

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
}

}

I was trying to implement something along the lines of what is already described here: Create Table with Variables in PowerShell

But there is no Output into the Azure Runbook console, but it does boot the VMs.

A very long winded explanation, but does anyone know how I could output to a Formatted Table inside a Workflow (Runbook) that will yield all my results in one table?

1
You biggest problem (only problem maybe) is that the JSON output is not valid JSON. Is that the native output? Can you modify that format?Johns-305
I gave up on the idea of outputting to JSON because of how it looks. As you said, it's not really valid JSON. So, I was trying to see if I can just output my results into 1 table that's not JSON, just normal PowerShell JSON and see if I can use that inside the Logic App. However, the code I have shown in the last section doesn't actually create a table output of both results into 1, it actually doesn't do anything at all. This is where I need some help, to create a table from both results (disregarding the JSON part).Beefcake
Typo "just normal PowerShell JSON" should be "just normal PowerShell Table" (don't type and walk at the same time!)Beefcake

1 Answers

0
votes

I ended up using an array to capture the info:

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
}

The last bit $result_array | ConvertTo-Json allowed me to get a better output which I hope to use in the Logic App. The output:

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

I don't like how it's an array of objects, cos now I gotta figure out how to get some braces around it { } within the Logic App to utilise it better.