AZ-140 Lab tips II:
I’m Amir Rouhanipoor, an IT Consultant specializing in Azure and cloud solutions. I help organizations streamline their IT and drive growth through secure, efficient cloud technologies.
PowerShell Script for VM Size Comparison
This script lists all session hosts with their host pool, VM name, and VM size across all host pools(loop through multiple resource groups):
Connect-AzAccount
# Get all host pools
$hostPools = Get-AzWvdHostPool
# Get all VMs in the subscription once
$allVMs = Get-AzVM
# Prepare output array
$allVMInfo = @()
foreach ($pool in $hostPools) {
$sessionHosts = Get-AzWvdSessionHost -ResourceGroupName $pool.ResourceGroupName -HostPoolName $pool.Name
foreach ($sessionHost in $sessionHosts) {
$vmName = $sessionHost.Name.Split("/")[-1]
# Search for the matching VM in all VMs
$vm = $allVMs | Where-Object { $_.Name -eq $vmName }
$vmSize = if ($vm) { $vm.HardwareProfile.VmSize } else { "Not Found" }
$vmRG = if ($vm) { $vm.ResourceGroupName } else { "Unknown" }
$allVMInfo += [PSCustomObject]@{
HostPool = $pool.Name
SessionHost = $sessionHost.Name
VMName = $vmName
VMSize = $vmSize
ResourceGroup = $vmRG
Status = $sessionHost.Status
}
}
}
# Display the result
$allVMInfo | Format-Table
# Optional: export to CSV
$allVMInfo | Export-Csv -Path "AVD_VM_Sizes.csv" -NoTypeInformation