List all the Classsic Azure VMs

Below script will list all the Classic 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
#$subscriptions = "Beta"
foreach($subscription in $subscriptions)
{
$subscription

  #  Select-AzureSubscription -Current -PassThru -SubscriptionId  (Get-AzureSubscription -SubscriptionName $subscription).SubscriptionId
  # Set-AzureRmContext -SubscriptionName $subscription.SubscriptionName
   #$account = $subscription | foreach-object {$_.defaultaccount}
   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.ResourceType = ($RmResource).ResourceType
        #$VirtualMachineInfo.VNET = $vm.VirtualNetworkName
        #$VirtualMachineInfo.Subnet = Get-AzureSubnet -VM $vm
        #$VirtualMachineInfo.size= $vm.InstanceSize
         $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