Create a vSwitch Port Overview with Powershell

WARNING: VMware vmotion does not check wether there are sufficient ports available on the virtual switch on the destination host. Migrating a vm to a host with insufficient ports will cause the vmotion to complete without warnings, yet the virtual NIC will be disconnected! This issue is descripbed in this KB article.
The solution to this problem is to create vSwitches with sufficient ports, obviously. Do you want to know how many ports are currently being used on every vSwitch in your environment? vSphere PowerCLI to the rescue! Try the following script:

$VC = Connect-VIServer "MyVCServer"
$myCol = @()
ForEach ($VMHost in (Get-VMHost | Sort Name))
	{
	ForEach ($VM in ($VMHost | Get-VM))
		{
		ForEach ($NIC in (Get-NetworkAdapter -VM $VM))
			{
			$myObj = "" | Select VMHost, VM, NIC, PortGroup, vSwitch
			$myObj.VMHost = $VMHost.Name
			$myObj.VM = $VM.Name
			$myObj.NIC = $NIC.Name
			$myObj.PortGroup = Get-VirtualPortGroup -VM $VM -Name $NIC.NetworkName
			$myObj.vSwitch = $myObj.PortGroup.VirtualSwitchName
			$myCol += $myObj
			}
		}
	}
$myCol | Group-Object VMHost, vSwitch -NoElement | Sort Name | Select Name, Count
Disconnect-VIServer -Confirm:$false
Close Menu