List all ARM VMs in azure

Below script will list all ARM VMs in Azure

 

Add-AzureAccount

Class VirtualMachineInfo
{
    [string]$ResourceType
    [string]$Subscription
    [string]$ResourceId
    [string]$ResourceGroup
    [string]$Location
    [string]$Name
    [string]$ServiceName
    [string]$VNet
    [string]$Subnet
    [string]$Size
    [string]$Status
    [string]$VMtype
    [string]$GuestName
    [string]$PrivateIP
    [string]$publicIP
    [string]$diskname
    [string]$fqdn 
    [string]$restype
    [string]$ostype
    [string]$monitoringagentextension
}

$allResources = @()
$filename = "c:\temp\azurereports\" + [string](get-azureaccount).ID.Split("@")[1] + "-CLASSIC.csv" 

$vms =@()
$subscriptions = Get-AzureSubscription
foreach($subscription in $subscriptions){
$subscription

   Select-AzureSubscription   -Subscriptionid $subscription.SubscriptionId  
   Write-Output "Processing data for subscription $Subscription.subscriptionname"
   $vms = Get-AzureVM 

foreach ($vm in $vms){
         $VirtualMachineInfo = New-Object VirtualMachineInfo
         $VirtualMachineInfo.Subscription = $subscription.SubscriptionName
         $VirtualMachineInfo.Name = $vm.Deploymentname
    
         $VirtualMachineInfo.Status = $vm.InstanceStatus
         $VirtualMachineInfo.restype = [string] "Classic VM"
       
         #$VirtualMachineInfo.PrivateIP = $vm.IpAddress
         #$VirtualMachineInfo.publicip = $vm.PublicIPAddress
         $VirtualMachineInfo.ostype= $vm.vm.OSVirtualHardDisk.OS

         $VirtualMachineInfo.ServiceName = $vm.servicename 

$uri=$vm.VM.OSVirtualHardDisk.MediaLink.AbsoluteUri
$VirtualMachineInfo.Location = Get-AzureDisk | Where-Object {$_.MediaLink -eq $uri}| Select-Object Location

$allResources += $VirtualMachineInfo | Select-Object `
            Subscription, `
            Name,   `
            Status, `
            Location , `
            Ostype , `
            Servicename
}

}
$allResources = $allResources | Sort-Object Subscription, ResourceType
$allResources | Export-Csv -Path $filename -Force