RSS

PowerCLI: Getting VMHosts, PortGroups and switches info

31 Mar

I needed to extract a list of hosts in vCenter and with them, get all the portgroups and switch info. Unfortunately, there is no one one-liner script that can do this. Mainly because you have standard and distributed switches and their properties are different. In order to do this you need to separate one-liner, both feeding into the same output file.

The first script output the hosts against their VDSwitches. The script creates a tab-delimited text file that you can easy copy and paste onto a spreadsheet. Both scripts output the following:
– Clustername
– Hostname
– virtual network adapter name
– IP
– subnet mask
– port group name
– switch name
– VLAN ID

Get-VMHostNetworkAdapter -vmkernel | 
% { $vnet=$_ ; get-vdportgroup -vmhostnetworkadapter $vnet | 
%{"$($vnet.vmhost.parent)`t$($vnet.vmhost)`t
$($vnet.name)`t$($vnet.ip)`t$($vnet.subnetmask)`t
$($_.name)`t$($_.vdswitch)`t$($_.vlanconfiguration)" | 
out-file .\temp\output.txt -append } }

The second script output the hosts against their standard switch, as you can see the order of the function is very different. The main reason is that VMHostNetworkAdapter object cannot be pipped into the VirtualPortGroup but it works the other way around.

You can also see from the second script the the names for virtualswitch and vlanID different from the above. Actually .vdswitch and .vlanconfiguration are objects themselves, but they are nice enough to output the values I want without needing to specify the actual object, i.e. switch name and VLAN ID.

Get-VirtualPortgroup -standard | 
% { $pg=$_ ; get-vmhostnetworkadapter -portgroup $pg | 
% { "$($_.vmhost.parent)`t$($_.vmhost)`t$($_.name)`t
$($_.ip)`t$($_.subnetmask)`t$($pg.name)`t
$($pg.virtualswitch.name)`t$($pg.vlanid)" |
 out-file .\temp\output.txt -append }}
Advertisements
 
Leave a comment

Posted by on March 31, 2017 in powershell, vmware

 

Tags: , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: