Search code examples
powershellazure-resource-managerazure-automationazure-runbook

Invoke-AzureRmVMRunCommand not returning anything in "Output" field when executing from Runbook


I'm trying to invoke a powershell script on to a Virtual Machine and retrieve the output of the script. I'm using the Invoke-AzureRmVMRunCommand cmdlet to invoke the script on the VM as shown below.

$ValidationResult = Invoke-AzureRmVMRunCommand -ResourceGroupName $VM.ResourceGroupName -VMName $VM.Name -CommandId "RunPowerShellScript" -ScriptPath $ValidationScript

When I execute the above cmdlet from a regular powershell terminal, I get the output as expected. However, whenever I'm putting this statement inside an automation runbook, I get null in almost all the fields as shown below

enter image description here

I don't see anything specific to this in documentation as well. Am I doing something wrong here?

Any help would be greatly appreciated! Thank you.

Update: In the script, I'm logging the output using Write-Output cmdlet.


Solution

  • My Apologies for the delayed response. I was using this in a runbook of type Powershell Workflow. Many of the PowerShell cmdlets behave differently when executed in workflows.

    So in this case, what was happening is the Invoke-AzureRmVMRunCommand was executing correctly but the response was only the TYPE of the response and not the actual response object. Hence I was unable to see any values in the response's properties.

    In order to make this work, I had to wrap the cmdlet call within an InlineScript {} block.

    $ValidationResult = InlineScript {
        $result = Invoke-AzureRmVMRunCommand -ResourceGroupName $USING:VM.ResourceGroupName -VMName $USING:VM.Name -CommandId "RunPowerShellScript" -ScriptPath $USING:ValidationScript
    
        $result.SubStatuses[0].Message
    }
    

    The result is returned to $ValidationResult variable.

    More detailed post is given here: https://amoghnatu.net/2018/04/15/get-output-of-script-executed-as-part-of-set-azurermvmcustomscriptexecution-cmdlet/

    Thanks.