Inventory Azure VMs
Documentation: Microsoft Learn
Get-AzVM with -Status queries each machine's instance view in addition to its model, and that is what populates the PowerState property.
The script
Script reader
Inspect before you run
Inspect the script below. It does not run in this browser.
Inspection only — no command has run.
Show reference output
Inspect this example’s structure
Read the command and its supplied arguments. This example requires real PowerShell or its module; this diagram does not execute it.
Get-AzVM -Status | Select-Object Name,ResourceGroupName,Location,PowerState- Stage 1Get-AzVM
-Status - Stage 2Select-Object
Positional values or no named parameters
How it works
Get-AzVM with -Status queries each machine's instance view in addition to its model, and that is what populates the PowerState property. The projection keeps the name with its resource group, region, and that power state.
Adapting it
Restrict the sweep with -ResourceGroupName when only one workload is in question, which also avoids making the extra status calls against everything else in the subscription.
Requirements
Az.Compute with a Connect-AzAccount session and Reader on the subscription.
If the result is unexpected
The VM inventory misses machines in other subscriptions.
Resource queries commonly use the current subscription context rather than every accessible subscription.
- Confirm the current subscription and explicitly iterate the approved subscription scope where required.
Validate the result
Each VM row identifies its subscription and resource group.
Keep the check reliable
Do not merge multi-subscription output without retaining those identifiers.
Primary documentation
Practise the script’s building blocks
Read Azure VM status
An Azure read context is preloaded in this fictional lab. Return the fictional Azure VM status inventory.
The sandbox is visible now. Sign in with Google only when you run or check a task; commands and temporary files are discarded when you leave.
Run a command to inspect its result.