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.

Linux
Migrate WordPress site to another hosting service.

IntroductionThis article details the migration of WordPress site (exactly this site you are now on) from one service provider to Amazon Lightsail. There might be various reason to do that (mine is outlined below) but in general I hope to share the message that especially with WordPress, migration can be …

VMware
VMware Workstation and Fusion can be installed and use for free (even for the enterprise)

For a while now, the VMware Workstation (and Fusion for MacOS) can be used without any additional fee for Personal use. That was a great Broadcom news and nice gesture from that software vendor. Recently Broadcom announced that the software will be available for all, even the commercial sector. This …

Linux
Salt, VMware implementation – part 1, introduction

As every IT administrator knows, the infrastructure (meaning storages, compute, VMware virtualisation stack) is just a fundaments to run various operating systems (OS) and finally (containerized) application. Therefore, installation of (let’s call it) infrastructure in the datacenter (SDDC), in that sense is just the beginning of the adventure. No wonder, …