This post explains how to retrieve the output of the script that is executed as part of Set-AzureRmVMCustomScriptExecution cmdlet.
The cmdlet adds a custom script virtual machine extension to a virtual machine. It lets users run custom scripts on the virtual machine. Now in order to retrieve the output if any, of the script that is executed, we can use the Get-AzureRmVMDiagnosticsExtension cmdlet with the -Name and -Status switch. Name holds the name of the custom script vm agent that was installed previously. Also, it is important to use the -Status switch here otherwise we won’t be able to retrieve the output.
You can see that the “SubStatuses“ property of the output of this cmdlet is of importance here and that this holds the output of the script that got executed for the above Set-AzureRmVMCustomScriptExecution cmdlet.
This property is an array which has 2 elements. First holds the output from StdOut stream and second from the StdErr stream. In this case, we’re interested in the StdOut stream.
As you can see, the “Message” property holds the output of the script that got executed on the VM as part of the CustomScript Execution cmdlet. In this case, it is just sample output.
If you’re looking for any errors, you can refer to the StdErr stream using SubStatuses array element.
A Catch here! An important one too!
If you’re running this code as part of an Azure Automation runbook of type “Workflow“, then remember that the cmdlet output is not an object but it’s just the names of the types. So the SubStatuses will only return the name of the TYPE of the property and NOT the actual object. This is how workflows are designed. So if you still want to retrieve the value, you need to execute this within an InlineScript tag as shown below. Click on picture to see clearly.
After the InlineScript tag completes execution, the variable $Result holds the value that is returned from the inline script tag, in this case, the output of the script executed as part of Custom Script execution cmdlet.
Hope this helps!