Azure / PowerShell / Get VM full details with subscription names

Get-AzSubscription | Select-AzSubscription | ForEach-Object {$_
$report = @()
$vms = Get-AzVM
$publicIps = Get-AzPublicIpAddress
$nics = Get-AzNetworkInterface | ?{ $_.VirtualMachine -NE $null}
foreach ($nic in $nics) {
$info = "" | Select-Object VmName, ResourceGroupName, Region, VirturalNetwork, Subnet, PrivateIpAddress, OsType, PublicIPAddress, SubscriptionName
$vm = $vms | ? -Property Id -eq $nic.VirtualMachine.id
foreach($publicIp in $publicIps) {
if($nic.IpConfigurations.id -eq $publicIp.ipconfiguration.Id) {
$info.PublicIPAddress = $publicIp.ipaddress
}
}
$info.OsType = $vm.StorageProfile.OsDisk.OsType
$info.VMName = $vm.Name
$info.ResourceGroupName = $vm.ResourceGroupName
$info.Region = $vm.Location
$info.VirturalNetwork = $nic.IpConfigurations.subnet.Id.Split("/")[-3]
$info.Subnet = $nic.IpConfigurations.subnet.Id.Split("/")[-1]
$info.PrivateIpAddress = $nic.IpConfigurations.PrivateIpAddress
$info.SubscriptionName = (Get-AzContext).Name
$report+=$info
}
$report | Export-CSV "D:\AzVMIPs.csv" -NoTypeInformation -Append
}

Tags:

2 Responses

  1. Hi Manu,

    How would I modify this script to import a csv with specific VM names to lookup?

    Thanks!

    • Hello Martin,
      Thanks for visiting my article and sending comments. The script can be changed as needed in the following line
      $vms = Get-AzVM.
      You need to import the csv file and loop through each line something similar as below:

      $vms = Import-CSV c:\list.csv
      foreach ($vm in $vms) {

      $vms = $vm.vmname

      }

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Comments
Add External Contacts in Microsoft Teams