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
Tagged with:
 

2 Responses to Create a vSwitch Port Overview with Powershell

  1. roger says:

    Great script, but could it be modified to include the number of configured ports on the vSwitch so that you can see directly from powershell whether you are over subscribed or not?
    e.g Name, No. Ports, Used Ports, Available ports

  2. roger says:

    something like this:

    $mycol = @()
    foreach ($vmhost in (get-vmhost | sort name))
    {
    foreach ($ports in ($vmhost | Get-VirtualSwitch | where {$_.name -eq “vSwitch2″}))
    {
    $myobj = “” | select VMhost, Ports, VMcount, Available
    $myobj.vmhost = $vmhost.name
    $myobj.vmcount = ($vmhost | get-vm | get-networkadapter).count
    $myobj.ports = $ports.numports
    $myobj.available = ($ports.numports – ($vmhost | get-vm | get-networkadapter).count)
    $mycol += $myobj
    }}
    $mycol

Leave a Reply