Export Azure VMS to csv file

Glenn Maxwell 11,841 Reputation points
2020-12-30T19:39:52.963+00:00

Hi All

i have 10 subscriptions and i want to export all the VMS to csv file.
i am using the below syntax to connect to Azure powershell. when i use this syntax it connects me only to one subscription.
Connect-AzAccount -Credential (get-credential -UserName myuser@Company portal .com)
When i use the syntax Get-AzVM i am getting only limited information. i want to export the below information of all VMs from all subscriptions to csv file.
experts guide me on this.

Subscription
Name
Location
Status
Resource Group
VMType
Core
Mem (MB)
OS
IP
Zone
Created

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
8,095 questions
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 111.5K Reputation points MVP
    2020-12-30T22:36:59.43+00:00

    Maybe this will help.

    $report = @()
    $subs = Get-AzSubscription
    Foreach ($sub in $subs)
        {
        select-AzSubscription $sub | Out-Null
        $subName = $sub.Name
    
        $vms = Get-AzVM
        $publicIps = Get-AzPublicIpAddress 
        $nics = Get-AzNetworkInterface | ?{ $_.VirtualMachine -NE $null} 
        foreach ($nic in $nics) { 
            $info = "" | Select VmName, ResourceGroupName, Region, VmSize, VirtualNetwork, PrivateIpAddress, OsType, PublicIPAddress, Subscription, Cores, Memory, CreatedDate
            $vm = $vms | ? -Property Id -eq $nic.VirtualMachine.id 
            foreach($publicIp in $publicIps) { 
                if($nic.IpConfigurations.id -eq $publicIp.ipconfiguration.Id) {
                    $info.PublicIPAddress = $publicIp.ipaddress
                    } 
                } 
                [string]$sku = $vm.StorageProfile.ImageReference.Sku
                [string]$os = $vm.StorageProfile.ImageReference.Offer
                $osDiskName = $vm.StorageProfile.OsDisk.Name
                $info.VMName = $vm.Name 
                $info.OsType = $os + " " + $sku
                $info.ResourceGroupName = $vm.ResourceGroupName 
                $info.Region = $vm.Location
                $vmLocation = $vm.location 
                $info.VmSize = $vm.HardwareProfile.VmSize
                $info.VirtualNetwork = $nic.IpConfigurations.subnet.Id.Split("/")[-3] 
                $info.PrivateIpAddress = $nic.IpConfigurations.PrivateIpAddress 
                $info.Subscription = $subName
                if ($vmLocation)
                    {
                    $sizeDetails = Get-AzVMSize -Location $vmLocation | where {$_.Name -eq $vm.HardwareProfile.VmSize}
                    }
                $info.Cores = $sizeDetails.NumberOfCores
                $info.Memory = $sizeDetails.MemoryInMB
                $osDisk = Get-AzDisk -ResourceGroupName $vm.ResourceGroupName -DiskName $osDiskName
                $info.CreatedDate = $osDisk.TimeCreated
                $report+=$info
                } 
        }
    $report | ft VmName, ResourceGroupName, Region, VmSize, VirtualNetwork, PrivateIpAddress, OsType, PublicIPAddress, Subscription, Cores, Memory, CreatedDate
    

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. whoward-msft 2,766 Reputation points
    2020-12-30T20:53:13.943+00:00

    Hi @Glenn Maxwell ,

    Once you connect to your Account, you can pull the data for each of the subscriptions that your account has access to. This medium article has an example of someone doing just that. You could tinker with this script to write the data out to a CSV file.

    Here is another example to get VM usages across subscriptions, again the process is the same once you have authenticated your account you can view the data for all subscriptions your account has access to.

    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.