Menu

Virtual Geek

Tales from real IT system administrators world and non-production environment

Powershell get the list of Azure Reservations Virtual Machines instances

As I was working on the Azure Reservations - PowerShell List All Azure Resverations. There was another requirement to provide the list of Virtual Machines usage under Azure Reservations by clients (which VMs are utilized under reservations), earlier another team was doing it manually to get the reports. Below screenshot is from one of the Azure Reservations resource Overview page.

Microsoft Access control azure portal reservations aggregate standard_M64s properties powershell az.reservations configuration create virtual machines reserved instances services scope discount free.png

If you scroll down a bit, You will find Virtual Machines (Resource names) used under Daily usage breakdown. I wanted to collect the same resources information list from multiple Azure Reservations.

Microsoft Access control azure portal reservations aggregate standard_M64s properties powershell az.reservations configuration create virtual machines reserved instances services scope discount free.png

Here in the script if you have multiple subscriptions it will go through the all subscriptions and fetch the resources list in the Azure Reservations (You can select the latest date to get current usage). List is saved in the csv with each subcription name.

Note: Make sure you have installed Az.Reservations module on the PowerShell.

#Install Az.Reservations Module if not exists.
#Install-Module -Name Az.Reservations -Scope CurrentUser

#Start and End date to fetch consumption usage details of Azure Reservations
$startDate = '04-01-2021'
$endDate = '05-10-2021'

#List all Azure subscriptions
$subscriptionIds = Get-AzSubscription
foreach ($subscription in $subscriptionIds) {
    #Change the Subscription
    Set-AzContext -Subscription $subscription
    #Get the information of Azure Reservation consumption usage details
    $info = Get-AzConsumptionUsageDetail -StartDate $startDate -EndDate $endDate
    #Save the information to Microsoft Excel CSV files with subscription id in the name of file
    $info | Export-Csv -NoTypeInformation -Path "C:\temp\$($subscription.name).csv"
}

Download this script here or it is also available on github.com.

Once reports are generated you can open it in the Excel and apply filter on the first row, On the product version filter the list to view which resources are consuming reservations. You can create Pivot table for better view and visibility.

Microsoft Azure Portal Reservations Virtual Machines Instance Name cost consumtion subscription scope Microsoft Excel csv powershell az module reservation instances resource group.png

Useful Articles
Part 1: Working With Azure Key Vault Using Azure PowerShell and AzureCLI
Part 2: Create a Virtual machine on Microsoft Azure
Part 3: Use a Azure VM system assigned managed identity to access Azure Key Vault
Create an Azure App registrations in Azure Active Directory using PowerShell & AzureCLI
Connect-AzureAD: One or more errors occurred. Could not load type 'System.Security.Cryptography.SHA256Cng'
Use Key Vault secret identifier url to get the secret value using Powershell
Working With Azure Key Vault Using Azure PowerShell and AzureCLI
Create key vault and secrets with access policies in Microsoft Azure
Creating a new user in Azure AD using oneliner PowerShell and Azure CLI
How to switch to other Azure AD tenant using PowerShell and Azure CLI

Go Back

Comment

Blog Search

Page Views

6346671

Follow me on Blogarama