Export Azure Virtual Machines details from all subscriptions

Following PowerShell script can be used to get the details like VmName ResourceGroupName Region VmSize OsType Subscription Cores Memory OSDiskSize

$report = @()
$subs = Get-AzSubscription
Foreach ($sub in $subs)
    {
    select-AzSubscription $sub | Out-Null
    $subName = $sub.Name

    $vms = Get-AzVM
    foreach ($vm in $vms) { 
        $info = "" | Select VmName, ResourceGroupName, Region, VmSize, OsType, Subscription, Cores, Memory, OSDiskSize
            [string]$sku = $vm.StorageProfile.ImageReference.Sku
            [string]$os = $vm.StorageProfile.ImageReference.Offer
            $osDiskName = $vm.StorageProfile.OsDisk.Name
            $info.VMName = $vm.Name
			$vmLocation = $vm.location 			
            $info.OsType = $os + " " + $sku
            $info.ResourceGroupName = $vm.ResourceGroupName
            $info.VmSize = $vm.HardwareProfile.VmSize
            $info.Subscription = $subName
            $sizeDetails = Get-AzVMSize -Location $vmLocation | where {$_.Name -eq $vm.HardwareProfile.VmSize}
            $info.Cores = $sizeDetails.NumberOfCores
            $info.Memory = $sizeDetails.MemoryInMB
			$disk = Get-AzDisk -DiskName $osDiskName -ResourceGroupName $vm.ResourceGroupName
			$info.OSDiskSize = $disk.DiskSizeGB
            $report+=$info
            } 
    }
$report | ft VmName, ResourceGroupName, Region, VmSize, OsType, Subscription, Cores, Memory, OSDiskSize

PowerShell Output should be like as below:

Tags:

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Comments