2

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.

Amogh Natu
  • 781
  • 1
  • 10
  • 37

3 Answers3

1

You need to either add the object name on last line of your script or use the Write-Output command. Otherwise it will not output anything.

The following lines both write an object to the output stream.

  • Write-Output –InputObject $ValidationResult

  • $ValidationResult

https://learn.microsoft.com/en-us/azure/automation/automation-runbook-output-and-messages

Hope this helps

Dave Anders
  • 799
  • 6
  • 13
0

This issue is most likely due to a reported bug in the AzureRM modules starting with version 5.7.0 / April 2018. Rolling back to version 5.6.0 / March 2018 is reported to fix it. Issue log: https://github.com/Azure/azure-powershell/issues/5982

Brian Reynolds
  • 402
  • 2
  • 5
0

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.

Amogh Natu
  • 781
  • 1
  • 10
  • 37