0
votes

I have a Powershell workflow runbook that automates starting and shutting down VMs in Azure, I updated the modules in an automation account (so I could use it for other things) and it has stopped the script working. I have fixed most of the broken stuff but the bit that is not now working is obtaining the power state eg: PowerState/deallocated so that it can be shutdown/started up. Here is my code:

$vmFullStatus = Get-AzureRmVM -ResourceGroupName test1 -Name test1 -Status
$vmStatusJson = $vmFullStatus | ConvertTo-Json -depth 100
$vmStatus = $vmStatusJson | ConvertFrom-Json
$vmStatusCode = $vmStatus.Statuses[1].code
Write-Output "     VM Status Code: $vmStatusCode"

The Write-Output VM Status Code is now blank in the output of the runbook, but it outputs fine in standard shell. I only have limited experiences in workflow runbooks but I believe it needs to be converted to Json so the Workflow can use it.

I think the issue may lie with the statuses as when it is converted to Json it displays:

"Statuses":  [
                 "Microsoft.Azure.Management.Compute.Models.InstanceViewStatus",
                 "Microsoft.Azure.Management.Compute.Models.InstanceViewStatus"
             ],

Which doesn't now show the PowerState. How can I get the powerstate of a vm from within a powershell workflow runbook so it can used? Thanks

2

2 Answers

0
votes

I have tried an inline script and it does work if you specify a vm name:

$vmStatusCode = InlineScript {
$vmFullStatus = Get-AzureRmVM -ResourceGroupName test1 -Name test1 -Status
$vmStatusJson = $vmFullStatus | ConvertTo-Json -depth 100
$vmStatus = $vmStatusJson | ConvertFrom-Json
$vmStatus.Statuses[1].code
}

But it doesn't work when you pass variables:

$vmFullStatus = Get-AzureRmVM -ResourceGroupName $vm.ResourceGroupName -Name $vm.Name -Status


Get-AzureRmVM : Cannot validate argument on parameter 'ResourceGroupName'. The argument is null or empty. Provide an 
argument that is not null or empty, and then try the command again.

it needs to be run without an inline script - any ideas?

0
votes

forgot to add $using:

$vmStatusCode = InlineScript {
                    $vmFullStatus = Get-AzureRmVM -ResourceGroupName $using:vm.ResourceGroupName -Name $using:vm.Name -Status
                    $vmStatusJson = $vmFullStatus | ConvertTo-Json -depth 100
                    $vmStatus = $vmStatusJson | ConvertFrom-Json
                    $vmStatus.Statuses[1].code
                    }

This now works!