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.

Free(BSD)
Search for duplicated files

This will be short (but hopefully I will find more time to show entire process to search duplicated files together with some examples). In case you are searching for duplicated files I can recommend two software which actually rocks in openSource world

Azure
NFS issue, cannot be mounted or is not visible

The same kind of issue I have encountered numerous times while working across different environments and with various customers. The problem with NFS mounts connected from remote locations is so common. This issue extends beyond communication solely over WAN and also include connections between datacenters (DC) where we lack control …

Azure
Why Firefox is important and people should use this browser in 2024, my thoughts.

Can you remember the times when everyone was using Internet Explorer? Back in the ’90s and the early part of this century, Internet Explorer dominated the browser market. Software Incompatibility with Other Browsers Incompatibility issues with software and other browsers have been a persistent problem. Even in 2022, this remains …