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, VMType
$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
$info.VMType = $vm.HardwareProfile.VmSize
$report+=$info
}
$report | Export-CSV "D:\AzureVMs.csv" -NoTypeInformation -Append
}
Post Views: 4,261
2 Responses
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
}