VMware Cloud Community
M5digisec
Contributor
Contributor
Jump to solution

Joining properties from multiple objects into a single table

I need to step through a collection of hosts and display a report with a handful of properties that I can display them in a row with each host.  These properties are all visible on the Configuration --> Networking tab of an ESXi host.  I would like to see something like this:

HOST       VMNIC      SPEED         DUPLEX        VSWITCH          PORT GROUP

host01     vmnic0     1000          True          vswitch0         management

host01     vmnic1     1000          True          vswitch0         management

host01     vmnic2     1000          True          vswitch1         vmotion

host01     vmnic3     1000          True          vswitch1         vmotion

host01     vmnic4     1000          True          vswitch2         vmprod

host01     vmnic5     1000          True          vswitch1         vmprod

host01     vmnic6     1000          True          vswitch3         backup

host01     vmnic7     1000          True          vswitch3         backup

host02     vmnic0     1000          True          vswitch0         management

etc...

I think is relatively simple, but I'm having trouble wrapping my brain around how to parse the objects properly in order to accomplish this.  I've been looking at the properties of various methods, and I believe the information I need to extract comes from these cmdlets with the properties listed after them:

Get-VMHostNetworkAdapter - DeviceName, BitRatePerSec, FullDuplex

Get-VirtualSwitch  - Name, Nic

Get-VirtualPortGroup - Name, VirtualSwitch

Seems like there should be a way to join the results of Get-VMHostNetworkAdapter and Get-VirtualSwitch using the "Device Name" and "Nic" properties respectively, and join the results of Get-VirtualSwitch and Get-VirtualPortGroup using the "Name" and "VirtualSwitch" properties respectively.  I just don't quite now how to piece all this information together.

Thanks for any suggestions.

/\/\ike


Tags (1)
Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

For non-distributed switches you can do it like this

foreach($esx in Get-VMHost){
    foreach($vsw in (Get-VirtualSwitch -VMHost $esx | where {$_.ExtensionData.GetType().Name -ne "DistributedVirtualSwitch"} ) ){
        foreach($pg in Get-VirtualPortGroup -VirtualSwitch $vsw){
            $pnics = $esx.ExtensionData.Config.Network.Pnic | where {$vsw.ExtensionData.Pnic -contains $_.Key}
            $pg | Select @{N="Host";E={$esx.Name}},
                @{N="pnic";E={[string]::Join(',',($pnics | %{$_.Device}))}},
                @{N="Speed";E={[string]::Join(',',($pnics | %{$_.LinkSpeed.SpeedMb}))}},
                @{N="Duplex";E={[string]::Join(',',($pnics | %{$_.LinkSpeed.Duplex}))}},
                @{N="vSwitch";E={$vsw.Name}},
                @{N="Portgroup";E={$pg.Name}}
                
        }
    }
}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
9 Replies
LucD
Leadership
Leadership
Jump to solution

For non-distributed switches you can do it like this

foreach($esx in Get-VMHost){
    foreach($vsw in (Get-VirtualSwitch -VMHost $esx | where {$_.ExtensionData.GetType().Name -ne "DistributedVirtualSwitch"} ) ){
        foreach($pg in Get-VirtualPortGroup -VirtualSwitch $vsw){
            $pnics = $esx.ExtensionData.Config.Network.Pnic | where {$vsw.ExtensionData.Pnic -contains $_.Key}
            $pg | Select @{N="Host";E={$esx.Name}},
                @{N="pnic";E={[string]::Join(',',($pnics | %{$_.Device}))}},
                @{N="Speed";E={[string]::Join(',',($pnics | %{$_.LinkSpeed.SpeedMb}))}},
                @{N="Duplex";E={[string]::Join(',',($pnics | %{$_.LinkSpeed.Duplex}))}},
                @{N="vSwitch";E={$vsw.Name}},
                @{N="Portgroup";E={$pg.Name}}
                
        }
    }
}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
M5digisec
Contributor
Contributor
Jump to solution

Lucd, thank you once again for the excellent answer. 

Being a newcomer to the use of PowerCLI I haven't worked with the ExtensionData property yet.  I see from the cmdlets reference that some objects I am using or planned to use such as VMHost and VirtualSwitch make use of this. Tell me a bit if you can about the use of the ExtensionData property and it's advantages/disadvantages (if any).

My host environment includes both standard switches as well as many vDS scattered about in various clusters.  What change would I need to make to accomodate both types of switches?

Kind regards,

/\/\ike

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The PowerCLI cmdlets return objects, let's call them .Net objects.

These objects contain a selection of properties and method selected by the PowerCLI Dev Team.

The reason that this is a selection is primarily because of performance reasons.

On the other hand there are the objects used in vSphere.

These are documented in the VMware vSphere API Reference Documentation.

Let's call these the server-side objects. They have all the documented properties and methods.

Sometimes you will need a property or a method that is not directly exposed through the PowerCLI cmdlets.

Then you will need to go to the server-side objects.

You can go from the .Net objects to the server-side objects with the Get-View cmdlet.

In one of the previous PowerCLI builds the ExtensionData property was introduced.

This gives you access to the server-side object in another way then with the Get-View cmdlet.

And the setup is quite intelligent. To avoid a waste of CPU cycles, the properties of the server-side object will only be fetched when you first address them.

I'll come back with a script to include dvSwitches as well.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

M5digisec
Contributor
Contributor
Jump to solution

Hi, LucD.  After running the example code I am seeing different results than what I expected.

I was thinking I would see 4 records for each host (one for each virtual swtich) showing the hostname and other data like this:

Host      : host1.mydomain.com
pnic      : vmnic6
Speed     : 1000
Duplex    : True
vSwitch   : vSwitch0
Portgroup : Mgmt

Host      : host1.mydomain.com
pnic      : vmnic7,vmnic11
Speed     : 1000,1000
Duplex    : True
vSwitch   : vSwitch1
Portgroup : vmot

Host      : host1.mydomain.com
pnic      : vmnic5,vmnic10
Speed     : 1000,1000
Duplex    : True
vSwitch   : vSwitch2
Portgroup : vmprod

Host      : host1.mydomain.com
pnic      : vmnic4,vmnic8
Speed     : 1000,1000
Duplex    : True
vSwitch   : vSwitch3
Portgroup : bkup

The results I get show 16 records for each host with the (above) correct pnic-vSwitch+Porgroup assignments as well as an additional 12 records for which the assignments are not correct.

Thank you,

/\/\ike

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The inner loop of that script is for the portgroups, so if you only have 1 portgroup per virtual switch your assumption of 4 records per host would be correct. Don't forget that the Console and Kernel connections are also considered portgroups.

What exactly is in those 12 additional records that shows incorrect assignments ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

M5digisec
Contributor
Contributor
Jump to solution

Thanks, LucD.

In the particular environment I am testing in each host has only a single port group per vSwitch, but other environments I have will include multiple port groups per vSwitch.

I have uploaded the output running the sample script against a single host in my test environment that shows the additional output.

Thank you,

/\/\ike

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Thanks, which particular portgroups are not needed ? Are these "Management Network" and "vmotion" ?

Those are in fact the Console and Kernel portgroups on ESXi servers.

Do you just want to exclude those from the output ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
M5digisec
Contributor
Contributor
Jump to solution

LucD, I do in fact want to see all the port groups including the two vmkernel vswitches. 

The actual assignments on the ESXi hosts in this environment are:

vswitch0 - Management Network - vmnic6

vswitch1 - vmotion - vmnic11,vmnic7

vswitch2 - production - vmnic10,vmnic5

vswitch3 - backup - vmnic8,vmnic4

The test output I uploaded shows these actual assignments, but for example the second record also shows vmnic6 connected to vswitch0 with the portgroup name of vmotion which is not how the assignments actually are. 

I've uploaded a screen cap which shows the vswitch, portgroup, and vmnic assignments for the host.

Thank you,

/\/\ike

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Ok, I think I found the problem, the Get-VirtualPortGroup cmdlet, when used with the -VMHost parameter, returns all the portgroups, independent if the -VirtualSwitch parameter is given.

I corrected the script above accordingly.

Please try again.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos