ferj
ferj

Reputation: 111

How to get the status of a list of VM in Azure using Powershell Get-Azvm

I am trying to get a list of Azure vm statuses (Allocated,Deallocated). I am able to get it one at a time for a single vm. But when i have a list of VM's or when using wildcard's it fails to get the vm status. Any tips

$ResGrp= "resvmtest"
$action="start"

 $vmList = Get-AzVM -ResourceGroupName $ResGrp -Name *  -Status

       foreach($vm in ($vmList | Select-Object @batch)){
  Write-Host $vm.Statuses[1].DisplayStatus 
         }

Upvotes: 1

Views: 9035

Answers (3)

jkavanagh58
jkavanagh58

Reputation: 21

This behaviour appears to have changed. Using the Status parameter now creates an object named Status and within that object.

One way to show the output: select Name, @{n='New';e={($_.Statuses | where Code -eq 'PowerState/running').DisplayStatus}}

Upvotes: 2

Stanley Gong
Stanley Gong

Reputation: 12153

Just try this :

Get-AzVm -ResourceGroupName <rg name> -Status | Select-Object Name, PowerState

Upvotes: 1

DivZ
DivZ

Reputation: 728

I see that you were trying to display the VMs in batches. Unless you have VMs in the thousands, that's not necessary. I also no longer see the Statuses property in Get-AzVM cmdlet. Just try this:

$vmList = Get-AzVM -ResourceGroupName $ResGrp -Status
$vmList | Select-Object Name, PowerState, ProvisioningState # or any other properties you want to display

Either that or I'm not understanding your question.

Upvotes: 3

Related Questions