How many VMs in each LAN?
A nice challenge today, when trying to answer a question in the VMware VI Toolkit community:
How to get an overview of all my networks and the number of VMs connected to each of them?
It’s unfortunately not possible to get VMs, based on which port group they are connected to. So something like this will NOT work: Get-VirtualPortGroup | Get-VM. Let’s try to do it the other way around: Get all the VMs we have. Get the portgroups they are connected to. And then figure out a way to invert the table.
First step: Getting the VMs and their port groups:
$VMs = Get-VM | Select Name, @{N=”PortGroups”;E={Get-VirtualPortGroup -VM $_ | %{$_.Name}}}
OK. That wasn’t too bad. The old Get-VM doing it’s job there. And the Get-VirtualPortGroup can return all port groups for each vm. We are only interested in the name of each port group, though. Using Calculated Properties we can stuff it into a single line.
Now to turn this table around. Piping to Group-Object does not quite deliver the desired result. That’s because some VMs have multiple port groups assigned. Group-Object then groups by each unique combination of port groups instead of per unique port group. Bummer. Let’s just try to list all unique port groups first then:
$PGs = $VMs | %{$_.PortGroups} | Select -Unique
Hey, that’s pretty simple. Now a small script can build a new collection of objects representing port groups for us. And we can definitely calculate how many vms there are associated to each port group, by using the $VMs collection we created earlier!
$myCol = @()
ForEach ($PG in $PGs)
{
$myObj = “” | Select PGName, NumVMs
$myObj.PGName = $PG
$myObj.NumVMs = ($VMs | Where {$_.PortGroups -contains $PG} | Measure-Object).Count
$myCol += $myObj
}
That ought to do the job. Let’s give it a nice, orderly view and we’re good to go!
$myCol | Sort NumVMs -Descending | Format-Table -AutoSize
Mystery solved!
3 Responses to How many VMs in each LAN?
Tags
Active Directory API bind order cleanup cluster CPU Custom Fields datastores description device management directory tree errors Event Log file name filter Fun function HA IT known issues License Server LUN multipath NIC objects Oneliner portgroups PowerCLI PowerShell profile recursive Registry Scripts security session share snapshots SQL Stat VI Toolkit VMware vSphere WMI WSUS ZenArchives
- July 2012
- July 2011
- February 2011
- January 2011
- December 2010
- May 2010
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008





Good point by Fabbio: It does not list the port groups with zero VMs. Thats because of the way I got the list of port groups. Of course you could easily do it this way:
$PGs = Get-VirtualPortGroup -VMHost (Get-VMHost “MyESXServer”) | %{$_.Name}
[...] How many VMs in each LAN? [...]
[...] A bit of exploration with get-member and I came up witha working script. It’s based on a post by Hugo Peeters and lists the VMs that are connected to (or belong to) a particular Port [...]