Powershell export virtual machines IP

This script is pretty handy, it will connect to the Virtual Center you specify in Powershell.

 

# you will need to adjust the viserver name
connect-viserver mecvm01.eu.kellogg.com

$reportedvms=New-Object System.Collections.ArrayList
$vms=get-view -viewtype virtualmachine |Sort-Object -Property { $_.Config.Hardware.Device | where {$_ -is [VMware.Vim.VirtualEthernetCard]} | Measure-Object | select -ExpandProperty Count} -Descending

foreach($vm in $vms){
$reportedvm = New-Object PSObject
Add-Member -Inputobject $reportedvm -MemberType noteProperty -name Guest -value $vm.Name
Add-Member -InputObject $reportedvm -MemberType noteProperty -name UUID -value $($vm.Config.Uuid)
$networkcards=$vm.guest.net | ?{$_.DeviceConfigId -ne -1}
$i=0
foreach($ntwkcard in $networkcards){
Add-Member -InputObject $reportedvm -MemberType NoteProperty -Name “networkcard${i}.Network” -Value $ntwkcard.Network
Add-Member -InputObject $reportedvm -MemberType NoteProperty -Name “networkcard${i}.MacAddress” -Value $ntwkcard.Macaddress
Add-Member -InputObject $reportedvm -MemberType NoteProperty -Name “networkcard${i}.IpAddress” -Value $($ntwkcard.IpAddress|?{$_ -like “*.*”})
Add-Member -InputObject $reportedvm -MemberType NoteProperty -Name “networkcard${i}.Device” -Value $(($vm.config.hardware.device|?{$_.key -eq $($ntwkcard.DeviceConfigId)}).gettype().name)
$i++
}
$reportedvms.add($reportedvm)|Out-Null
}

$reportedvms|Export-Csv c:myreport.csv