PowerCLI code to gather IP and portGroup for VM

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:

  1. 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
  2. for each vm in that file we are creating object with the following values:
    * IP address from vm.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

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

VMware
VCF, backup configuration

Backup implementation for VMware component is fairly easy. Just the requirements is to configure SFTP server in proper way and make it network available to the VMware components. SDDC Manager and NSX Manager backup In VCF Operations it is possible to configure backup for SDDC Manager and NSX Manager. Go …

VMware
VCF SoS

SoS (Supportability and Serviceability) command can be used for troubleshooting purpose to generate VCF (per component) log bundle, massively enable/disable ssh service on ESXi, vCenter, password and certificates expiry status, verify cluster health and many other. while troubleshooting, the following commands can be helpful:

Free(BSD)
Linux text manipulation

This article will grow over time Definition Command 1 Remove all comments including empty line (comments with ; and # like in samba.conf) egrep -v ‘^[[:space:]]*$|^ *#|^ *;’ /etc/samba/smb.conf 2 Find all file with some extension and remove it one by one; works for gnu xargs; 0 argument to properly …