Reputation: 409
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?
Upvotes: 3
Views: 2240
Reputation: 643
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)]"
}
}
Upvotes: 1
Reputation: 409
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 :
Upvotes: 3