I'm attempting to get a list of all my Azure VMs in Powershell.
Get-AzureVM Unfortunately this only returns the VMs listed under Virtual machines (classic).
How can I get a list of the new Virtual machines?
Based on David's answer, I wrote the following script that combines the two lists of VMs:
Switch-AzureMode -Name AzureServiceManagement #ResourceGroupName will be blank for these $classicVms = Get-AzureVM | select Name, ServiceName, ResourceGroupName Switch-AzureMode -Name AzureResourceManager #ServiceName will be blank for these $armVms = Get-AzureVM | select Name, ServiceName, ResourceGroupName $allVms = $classicVms + $armVms $allVms When you run this, you'll get a warning that Switch-AzureMode is deprecated.
WARNING: The Switch-AzureMode cmdlet is deprecated and will be removed in a future release The deprecation is part of a breaking change. You can read the details here: Deprecation of Switch-AzureMode.
You need to use the Azure Resource Manager mode to access the new VMs:
Switch-AzureMode -Name AzureResourceManager You can read more about it here.
Note that Switch-AzureMode has now been deprecated (https://github.com/Azure/azure-powershell/wiki/Deprecation-of-Switch-AzureMode-in-Azure-PowerShell). Cmdlet Rename All cmdlets under Azure Resource Management modules will be renamed to fit the following format: [Verb]-AzureRm[Noun]
Example: New-AzureVm becomes New-AzureRmVm
Using the Azure CLI, we can use the az vm list command to get a list of all VMs in the current subscription. Adding on this, we just loop over all our subscriptions and add the results to a single list
$results=New-Object -TypeName System.Collections.ArrayList; $subs = az account list | ConvertFrom-Json; $subIndex = 0; foreach ($sub in $subs) { $subIndex++; Write-Progress "Gathering VMs" -Status $sub.name -PercentComplete ($subIndex / $subs.Count * 100) $vms = az vm list --subscription $sub.id | ConvertFrom-Json $results.Add(@{ subscription = $sub vms = $vms }) > $null; $results | ConvertTo-Json -Depth 100 > vms.json; } This, however, does not include the power on/off state of the vms
We can get all the VM info + the power state using the az graph query command. This has the benefit of being even faster. A little work is needed to process the paginated results, but it's still fairly easy.
$query = " Resources | where type == 'microsoft.compute/virtualmachines' | extend PowerState = tostring(properties.extended.instanceView.powerState.code) | extend vmSize = tostring(properties.hardwareProfile.vmSize) "; $params = @( "--graph-query", $query -replace "`n", "" "--output", "json" ); $total=New-Object -TypeName System.Collections.ArrayList; $count=0; while ($true) { $results = az graph query @params | ConvertFrom-Json; foreach ($d in $results.data) { $total.Add($d); } $count += $results.count; $params = @( "--graph-query", $query -replace "`n", "" "--output", "json" "--skip-token", $results.skip_token "--skip", $count ); if ([string]::IsNullOrEmpty($results.skip_token)) {break;} } $total | ConvertTo-Json -Depth 100 > vms_fast.json; Note that we use array splatting instead of object splatting