Integrating Azure Automation Runbook Output with Logic Apps

This story was originally published on my blog @ https://www.singhkays.com/blog/azure-automation-runbook-output-logic-apps/

Problem

Here’s an example AzureRM resource Id for reference

/subscriptions/12345678-abcd-4179–8ab1-sda1231sada/resourcegroups/some-rg/providers/Microsoft.Compute/disks/somedisk

The expected output of the above should be

Resource Group Name   = some-rgResource Name         = somediskSubscription Name     = Visual Studio Ultimate with MSDN

**NOTE** above that I’d like to get the subscription name for the corresponding subscription Id I get from the resource Id.

Know before you begin

/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{resourceProviderNamespace}/{resourceType}/{resourceName}

Using the above knowledge, parsing out the required fields is relatively trivial using one of the string manipulation functions in your favorite scripting/programming language. However, because we need to make an authenticated call to Azure to get the name of the subscription for the corresponding subscription Id, I decided to use Azure Automation. The benefit of Azure Automation over another serverless service like Azure Functions is that Azure PowerShell modules are available without you having to figure out the installation.

Solution

param([string]$id)$connectionName = "AzureRunAsConnection"try{  # Get the connection "AzureRunAsConnection "  $servicePrincipalConnection=Get-AutomationConnection -Name    $connectionName  Add-AzureRmAccount `    -ServicePrincipal `    -TenantId $servicePrincipalConnection.TenantId `    -ApplicationId $servicePrincipalConnection.ApplicationId `    -CertificateThumbprint  $servicePrincipalConnection.CertificateThumbprint | Out-Null}catch {    if (!$servicePrincipalConnection)    {       $ErrorMessage = "Connection $connectionName not found."       throw $ErrorMessage    } else{      Write-Error -Message $_.Exception      throw $_.Exception    }}$arr = $id.Split("/")
# Array indexes correspond to the elements in the resource Id format above$rg = $arr[4]$diskName = $arr[8]$subId = $arr[2]# Get the subscription name$subName = (Get-AzureRmSubscription -SubscriptionId $subId).Name# Create a JSON object for output to next step in Logic App workflow$objOut = [PSCustomObject]@{ResourceGroupName = $rgDiskName = $diskNameSubscriptionName = $subName}Write-Output ( $objOut | ConvertTo-Json)

Here’s how the output of the above runbook looks like when passed in a resource Id for a managed disk:

In the Logic Apps designer, you’ll call this runbook using the “Create Job” Azure Automation connector. After the job is completed, you can get the output using the “Get Job” Azure Automation connector. The parameters that need to be specified are shown below in the image. The most important is the “Job Id” which identifies the job you kicked off.

After this you’ll need to parse the JSON object returned from the runbook. To do this add the “Parse JSON” connector.

The content parameter takes in the output passed in from the previous step. However, to parse this correctly you need to specify the schema. Fortunately, generating the schema is as simple as clicking the “Use sample payload to generate schema” link shown above and pasting in the output from the previous step

After pasting in the output, hit “Done” and you’ll see that the correct schema has been generated

And that’s it! After you parse the JSON, you can use the properties by their name in any follow up actions in your Logic App workflow. For example, I use these properties in the subject of an email.

PM @ Azure Compute buidling Managed Disks and Shared Image Gallery - All opinions and blogs my own and in no way official documentation.