3

I am using an Azure PowerShell Runbook to execute a PowerShell script on an Azure virtual machine. I do not find a way to get the output of the remote script when I'm using the Azure resource manager functions, which I have to use for my deployment. There are a lot examples using the 'non-resource manager' way, which looks like this:

# Execute remote script
$Vm = Get-AzureVM -ServiceName "DSCDemo" -Name "DSCPull"
Set-AzureVMCustomScriptExtension -ContainerName scripts -StorageAccountName psmag -FileName user.ps1 -Run user.ps1 -VM $vm | Update-AzureVM -Verbose
# Get output
$vm = Get-AzureVM -ServiceName DSCDemo -Name DSCPull
$output = $Vm.ResourceExtensionStatusList.ExtensionSettingStatus

The $output variable then contains the standard and error output of the script that has been executed. The same code looks pretty similar for my resource manager version:

# Execute remote script
$vm = Get-AzureRmVM -Name "DSCPull" -ResourceGroupName $ResourceGroupName
$result = Set-AzureRmVMCustomScriptExtension -ResourceGroupName $ResourceGroupName `
                                                -VMName "DSCPull"  `
                                                -Name 'user' `
                                                -Location $vm.Location  `
                                                -StorageAccountName psmag  `
                                                -StorageAccountKey '<key>' `
                                                -FileName "user.ps1"  `
                                                -ContainerName "scripts"  `
                                                -RunFile "user.ps1"
$output = Get-AzureRmVM -Name $VMName -ResourceGroupName $ResourceGroupName -Status

But the output is completely different and I do find anything that contains the standard output or error output.

How do I retrieve the output with help of the Azure resource manager functions?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
MoJo2600
  • 409
  • 3
  • 10

2 Answers2

3

OK, I found the answer! You can always query the result with help of the Get-AzureRmVmDiagnosticExtension command:

$output = Get-AzureRmVMDiagnosticsExtension -ResourceGroupName $ResourceGroupName -VMName 'DSCPull' -Name 'user' -Status
$output.SubStatuses[0]
$output.SubStatuses[1]

Will return something like

Code          : ComponentStatus/StdOut/succeeded
Level         : Info
DisplayStatus : Provisioning succeeded
Message       : my output on remote
Time          :


Code          : ComponentStatus/StdErr/succeeded
Level         : Info
DisplayStatus : Provisioning succeeded
Message       :
Time          :
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
MoJo2600
  • 409
  • 3
  • 10
  • 1
    Just to add to your answer and to save some time for others who may face this issue, please note, this cmdlet works fine when running from powershell terminal but in case you're running it from automation runbook of type "Workflow", this doesn't return expected output. To get the output, you need to wrap it within an InlineScript{} block and return the output from the block. Check URL for details http://amoghnatu.net/2018/04/15/get-output-of-script-executed-as-part-of-set-azurermvmcustomscriptexecution-cmdlet/ – Amogh Natu Apr 17 '18 at 02:42
1

In my testing, it can also be retrieved using Get-AzureRmVMExtension, which is arguably the more logical one to use. You must include the -Status parameter, else you don't get the status and substatus values back.

Also, if retrieving it in the output section of a Resource Manager template, something like this works (although I'm not fond of the hard-coded zero index):

"outputs": {
    "foo": {
        "type": "string",
        "value": "[reference('Microsoft.Compute/virtualMachines/my-vm/extensions/my-script').instanceView.substatuses[0].message)]"
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
John Rusk - MSFT
  • 613
  • 5
  • 10