How can I retrieve and export the Azure VM list using PowerShell without limiting the result to the first 100 VMs ?

EnterpriseArchitect 6,041 Reputation points
2024-02-20T05:59:33.2133333+00:00

I need help retrieving all of my Azure VM lists under my specific subscriptions where the total VMs is more than 100.

WARNING: There are more than 100 VMs in the result.  Only the statuses of 100 VMs are shown to avoid throttling.  To get the actual status of each VM, please provide a VM name with -Status parameter.

I got this warning and the result is only showing for the first 100 VMs out of 1000+ VMs I must retrieve the list.

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
9,013 questions
Windows for business Windows Server User experience PowerShell
Microsoft Security Microsoft Entra Microsoft Entra ID
Microsoft Security Microsoft Graph
{count} votes

Accepted answer
  1. TP 124.7K Reputation points Volunteer Moderator
    2024-02-20T11:58:13.5633333+00:00

    Hi,

    I made quick modification to your existing script. Please take a look and test to see if it works okay. It can be cleaned up and reworked, but for now see if it works without the error you were seeing. It isn't designed to handle multiple NICs.

    $report = @()  
    $sleepDuration = 200 
    $vmResources = Get-AzResource -ResourceType Microsoft.Compute/virtualmachines
    foreach ($res in $vmResources) {
        $VMs = Get-Azvm -ResourceGroupName $res.ResourceGroupName -Name $res.Name 
        Start-Sleep -Milliseconds $sleepDuration
    foreach ($vm in $VMs) {  
        $nic = Get-AzNetworkInterface -ResourceId $vm.NetworkProfile.NetworkInterfaces.Id
        $info = "" | Select-Object ResourceGroupName, Name, ComputerName, Location, VmSize, OsType, NIC, PublicIpAddress, PrivateIpAddress, ProvisioningState, Zone, PowerState, MaintenanceAllowed  
        $info.Name = $vm.Name  
        $info.ComputerName = (Get-AzVm -ResourceGroupName $vm.ResourceGroupName -Name $vm.Name -Status).ComputerName
        $info.ResourceGroupName = $vm.ResourceGroupName  
        $info.Location = $vm.Location  
        $info.VmSize = $vm.hardwareProfile.vmSize  
        $info.OsType = $vm.storageProfile.osDisk.OsType  
        $CharArray = $vm.networkProfile.networkInterfaces.id.Split("/")  
        $info.NIC = $CharArray[$CharArray.count - 1] 
        if ($null -ne $nic.IpConfigurations.PublicIpAddress) {  
            $pipRes = Get-AzResource -ResourceId $nic.IpConfigurations.PublicIpAddress.Id
            $info.PublicIpAddress = $pipRes.properties.ipAddress  
        }
        $info.PrivateIpAddress = $nic.IpConfigurations.PrivateIpAddress
        $info.ProvisioningState = $vm.ProvisioningState  
        $info.Zone = $vm.Zones  
        $info.PowerState = $vm.PowerState  
        $info.MaintenanceAllowed = $vm.MaintenanceAllowed  
        If ($null -eq $vm.PowerState) {  
            $vmNew = Get-Azvm -Name $vm.Name -Status  
            $info.PowerState = $vmNew.PowerState  
        }  
      
        $report += $info  
    }  
    }
    $report | Format-Table ResourceGroupName, Name, ComputerName, Location, VmSize, OsType, NIC, PublicIpAddress, PrivateIpAddress, ProvisioningState, Zone, PowerState, MaintenanceAllowed
    
    

    Please click Accept Answer and upvote if the above was helpful.

    Thanks.

    -TP

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

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.