Azure PowerShell – get VM usage from across all subscriptions

Mixing the Azure PowerShell module and Azure CLI could be causing issues with your code if the accounts haven’t been retrieved between the two. Verify that az cli has the proper subscriptions

az account list -o table

If you don’t see the accounts be sure to re-run az login.

Here’s your code with the azure cli only


$VMs = @()
az account list -o json | ConvertFrom-Json |
    ForEach-Object {
        Write-Host "Getting usage for account: " $_.Name
az account set -s $_.Name
    $VMs += (az consumption usage list --start-date "2020-07-01" --end-date "2020-07-03" | ConvertFrom-Json)

$VMs | Where-Object {$_.product -Match "Virtual Machines"} |
    Sort-Object -Property instanceName -Descending |
        Select-Object instanceName, subscriptionName |
            Get-Unique -AsString | ConvertTo-Csv -NoTypeInformation |
                Set-Content $file

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top