Retrieving Azure VM SKU across all Subscriptions not returning actual VM name and SKU type

Yasir Haider 25 Reputation points
2024-08-02T15:18:43.6266667+00:00

Hi Everyone,

I am trying to retrieve Azure VM SKUs with VM Name iterating through all subscriptions in our environment. Attached is my power shell code. Results are coming up as skewed for some reason and instead of vm name, size info it outputs only extended properties column as a string without any actual vm name, sku info.

User's image

Can anyone please provide some guidance as what I might be missing here?

Thanks in advance!

# Install the Azure PowerShell module if not already installed
# Install-Module -Name Az -AllowClobber -Scope CurrentUser

# Import the Az module
# Import-Module Az

# Connect-AzAccount

# Function to get VM names and sizes from a subscription
function Get-VMNamesAndSizes {
    param (
        [string]$SubscriptionId
    )

    # Set the subscription context
    Set-AzContext -SubscriptionId $SubscriptionId

    # Get all VMs in the subscription
    $vms = Get-AzVM

 # Iterate through each VM and output the VM name, size, and extended properties
 $vmDetails = @()
 foreach ($vm in $vms) {
     $vmProps = @{
         VMName            = $vm.Name
         VMSize            = $vm.HardwareProfile.VmSize
         ResourceGroupName = $vm.ResourceGroupName
         Location          = $vm.Location
         OS                = $vm.StorageProfile.OSDisk.OSType
         IPAddress         = $null
     }

    
     # Extract extended properties
     if ($vm.Tags) {
         foreach ($tag in $vm.Tags.Keys) {
             $vmProps["Tag_$tag"] = $vm.Tags[$tag]
         }
     }

     $vmDetails += [PSCustomObject]$vmProps
 }

 return $vmDetails

}


# Get the list of all subscriptions
$subscriptions = Get-AzSubscription

# Specify the number of subscriptions to iterate through
$subscriptionLimit = 10

# Initialize a counter
$counter = 3

# Initialize an array to store all VM details
$allVMDetails = @()

# Iterate through the subscriptions up to the specified limit
foreach ($subscription in $subscriptions) {
    if ($counter -ge $subscriptionLimit) {
        break
    }

    # Get VM names and sizes for the current subscription and add to the array
    $allVMDetails += Get-VMNamesAndSizes -SubscriptionId $subscription.Id

    # Increment the counter
    $counter++
}

# Specify the output file path
$outputFilePath = "C:\Users\Documents\vmsku.csv"

# Export the results to a CSV file
$allVMDetails | Export-Csv -Path $outputFilePath -NoTypeInformation

Write-Output "VM details have been exported to $outputFilePath"



Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
9,044 questions
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Manu Philip 20,206 Reputation points MVP Volunteer Moderator
    2024-08-02T16:25:20.1933333+00:00

    Hi,

    I would suggest you check the following article in my blog to get a simple PS script to help on the same purpose and withe the desired output you are looking for

    Azure / PowerShell / Get VM full details with subscription names

    Hope this helps.


    --please don't forget to upvote and Accept as answer if the reply is helpful--

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Yasir Haider 25 Reputation points
    2024-08-02T19:42:41.5566667+00:00
    Import-Module Az.Accounts
    Import-Module Az.Resources
    Import-Module Az.Compute
    Import-Module Az.Monitor
    
    # Specify the number of subscriptions to iterate through
    $subscriptionLimit = 10
    
    # Initialize a counter
    $counter = 1
    
    # Initialize the report array
    $report = @()
    
    # Get the list of all subscriptions
    $subscriptions = Get-AzSubscription | Where-Object { $_.State -eq 'Enabled' }
    
    Foreach ($sub in $subscriptions) {
        if ($counter -ge $subscriptionLimit) {
            break
        }
    
        # Set the subscription context
        Select-AzSubscription -Subscription $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
            $info.VMName = $vm.Name
            $vmLocation = $vm.Location          
            $info.ResourceGroupName = $vm.ResourceGroupName
            $info.Region = $vmLocation
            $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
            $info.OsType = $vm.StorageProfile.OSDisk.OSType
            $info.OSDiskSize = $vm.StorageProfile.OSDisk.DiskSizeGB
            $report += $info
        }
    
        # Increment the counter
        $counter++
    }
    
    $report | Format-Table VmName, ResourceGroupName, Region, VmSize, OsType, Subscription, Cores, Memory, OSDiskSize
    
    # Specify the output file path
    $outputFilePath = "C:\Users\Documents\xyz.csv"
    
    # Export the results to a CSV file
    $report | Export-Csv -Path $outputFilePath -NoTypeInformation
    
    Write-Output "VM details have been exported to $outputFilePath"
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.