Hi,
I made quick modification to your existing script. Please take a look and test to see if it works okay. It can be cleaned up and reworked, but for now see if it works without the error you were seeing. It isn't designed to handle multiple NICs.
$report = @()
$sleepDuration = 200
$vmResources = Get-AzResource -ResourceType Microsoft.Compute/virtualmachines
foreach ($res in $vmResources) {
$VMs = Get-Azvm -ResourceGroupName $res.ResourceGroupName -Name $res.Name
Start-Sleep -Milliseconds $sleepDuration
foreach ($vm in $VMs) {
$nic = Get-AzNetworkInterface -ResourceId $vm.NetworkProfile.NetworkInterfaces.Id
$info = "" | Select-Object ResourceGroupName, Name, ComputerName, Location, VmSize, OsType, NIC, PublicIpAddress, PrivateIpAddress, ProvisioningState, Zone, PowerState, MaintenanceAllowed
$info.Name = $vm.Name
$info.ComputerName = (Get-AzVm -ResourceGroupName $vm.ResourceGroupName -Name $vm.Name -Status).ComputerName
$info.ResourceGroupName = $vm.ResourceGroupName
$info.Location = $vm.Location
$info.VmSize = $vm.hardwareProfile.vmSize
$info.OsType = $vm.storageProfile.osDisk.OsType
$CharArray = $vm.networkProfile.networkInterfaces.id.Split("/")
$info.NIC = $CharArray[$CharArray.count - 1]
if ($null -ne $nic.IpConfigurations.PublicIpAddress) {
$pipRes = Get-AzResource -ResourceId $nic.IpConfigurations.PublicIpAddress.Id
$info.PublicIpAddress = $pipRes.properties.ipAddress
}
$info.PrivateIpAddress = $nic.IpConfigurations.PrivateIpAddress
$info.ProvisioningState = $vm.ProvisioningState
$info.Zone = $vm.Zones
$info.PowerState = $vm.PowerState
$info.MaintenanceAllowed = $vm.MaintenanceAllowed
If ($null -eq $vm.PowerState) {
$vmNew = Get-Azvm -Name $vm.Name -Status
$info.PowerState = $vmNew.PowerState
}
$report += $info
}
}
$report | Format-Table ResourceGroupName, Name, ComputerName, Location, VmSize, OsType, NIC, PublicIpAddress, PrivateIpAddress, ProvisioningState, Zone, PowerState, MaintenanceAllowed
Please click Accept Answer and upvote if the above was helpful.
Thanks.
-TP