Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Get the compute resource details for VM and Disk into an Excel file
#Requires -Modules ImportExcel, Az.Accounts, Az.Compute
<#
.SYNOPSIS
Retrieve region specific Sku details for Virtual Machines and Managed Disks into an Excel file
.EXAMPLE
./Get-AzComputeResourceData.ps1 -Regions 'westus2','westus3' -ExcelFileName 'c:\temp\azComputeResourceSku.xlsx
#>
[cmdletbinding()]
param(
# Provide list of regions to get data against, defaults: westus2, westcentralus
[string[]]
$Regions = ('westus2','westcentralus'),
# Provide full file path to Excel file, defaults <CurrentDirectory>\azComputeResourceSkus.xlsx
[string]
$ExcelFileName = ([IO.Path]::Join($PSScriptRoot,'azComputeResourceSkus.xlsx'))
)
if (Test-Path $excelFileName) {
Remove-Item $excelFileName -Force
}
if (-not (Get-AzContext)) {
Connect-AzAccount
}
$data = Get-AzComputeResourceSku | Where-Object Locations -In $regions
$vmData = $data | Where-Object ResourceType -EQ 'virtualMachines'
$diskData = $data | Where-Object ResourceType -EQ 'disks'
$vmData | ForEach-Object {
$vmDetail = $_
$details = $vmDetail.Capabilities.GetEnumerator()
$result = [ordered]@{
Name = $vmDetail.Name
Tier = $vmDetail.Tier
Size = $vmDetail.Size
Location = $vmDetail.LocationInfo.Location
Zones = ($vmDetail.LocationInfo.Zones | Sort-Object) -join '|'
}
foreach ($d in $details) {
$result.Add($d.Name,$d.Value)
}
[pscustomobject]$result
} | Export-Excel -Path $excelFileName -ClearSheet -WorksheetName 'VirtualMachines' -AutoSize
$diskData | ForEach-Object {
$diskDetail = $_
$details = $diskDetail.Capabilities.GetEnumerator()
$result = [ordered]@{
Name = $diskDetail.Name
Tier = $diskDetail.Tier
Size = $diskDetail.Size
Locations = $diskDetail.LocationInfo.Location
Zones = ($diskDetail.LocationInfo.Zones | Sort-Object) -join '|'
}
foreach ($d in $details) {
$result.Add($d.Name,$d.Value)
}
[pscustomobject]$result
} | Export-Excel -Path $excelFileName -ClearSheet -WorksheetName 'Disks' -AutoSize
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment