It is not an easy task to collect those two information together as it can be read from two different objects. It is always an issue (for me) to merge two value from two different object as VM can have several network cards and at the same time be connected to the same portGroup – or of course, to different ones.
Below you can find the code that should work for VMs with VMtools installed and that have up to 3 network cards:
The code can be used at your own risk.
$vms = Get-Content .\Desktop\vm_list.txt
foreach ($vmm in $vms){
$vmm = get-vm $vmm
$vmobj = New-Object -TypeName psobject
$vmobj | Add-Member -MemberType NoteProperty -Name “name” -Value $vmm.name
$vmobj | Add-Member -MemberType NoteProperty -Name “ipaddress0” -Value $vmm.Guest.Nics[0].IPAddress[0]
$vmobj | add-member -MemberType NoteProperty -Name “pg0” -value ($vmm | get-virtualportgroup | where {$_.key -ccontains $vmm.Guest.Nics[0].NetworkName }).name
if((($vmm).Guest.Nics).count -gt 1){
$vmobj | Add-Member -MemberType NoteProperty -Name “ipaddress1” -Value $vmm.Guest.Nics[1].IPAddress[0]
$vmobj | add-member -MemberType NoteProperty -Name “pg1” -value ($vmm | get-virtualportgroup | where {$_.key -ccontains $vmm.Guest.Nics[0].NetworkName }).name }
if((($vmm).Guest.Nics).count -gt 2){
$vmobj | Add-Member -MemberType NoteProperty -Name “ipaddress2” -Value $vmm.Guest.Nics[2].IPAddress[0]
$vmobj | add-member -MemberType NoteProperty -Name “pg2” -value ($vmm | get-virtualportgroup | where {$_.key -ccontains $vmm.Guest.Nics[2].NetworkName }).name}
$vmobj | select *
}
Explanation:
- at the beginning lists of VM can be read (from Destkop\vm_list.txt in that example). That list should contans VMs name, every value per row
- for each vm in that file we are creating object with the following values:
* IP address fromvm.guest.nics[0].ipaddress[0]
* portGroup (pg) from:($vm | get-virtualportgroup | where {$_.key -ccontains $vmm.Guest.Nics[2].NetworkName }).name
When this code will not work (completely):
In scenario when VM has more than one IP per network card it will take just first value.
If you would like to have more comprehensive information related to the network information from your vCenter as usual I can recommend to use rvtools or liveoptics
[1] rvtools (https://www.robware.net/rvtools/download/)
[2] live optics (liveoptics.com)
No Comments