Home > Powershell, Toolbox, VMWare > Useful scripts for amending the number of ports per vSwitch

Useful scripts for amending the number of ports per vSwitch

We have found that on many (most) of our ESX hosts, we run at risk of running our of vbSwitch ports when we run through our maintenance periods and reduce the number of hosts in a cluster.
Below are the scripts that I use to amend these settings:

# Calculating ports in use on each vSwitch: 
$myCol = @() 
ForEach ($VMHost in (get-cluster "prod Core" | 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 

“To upgrade the port numbers on vSwitch1 on myesxhost.intra to 120 ports , you need:”

# To increase number of ports on a vSwitch 
Get-VMHost "myserver1.intra" | % {Get-VirtualSwitch -VMHost $_ -Name vSwitch0 | where {$_.NumPorts -lt "128"}} | % { Set-VirtualSwitch -VirtualSwitch $_ -NumPorts "128" } 

“Note, the PowerCli says ‘128’ – the reason for this is that VMware reserves 8 ports for overhead on each vswitch.
(http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1008040)

You’ll also want to vMotion the Vms off each host – you can highlight multiple VMs and drag and drop, or right click, select ‘migrate’ and follow the wizard.
If you want to PowerCli it, you can try the following:”

# For a controlled migration. moving all VMs on myserver1 to myserver2 
get-vmhost myserver1.intra | get-vm | move-vm -destination (get-vmhost myserver2.intra) 

“The above will prompt you on each VM – you can ignore all prompts with :”

# to Ignore prompts 
get-vmhost myserver1.intra | get-vm | move-vm -destination (get-vmhost myserver2.intra) -Confirm:$false 

We previously had a bug that NICs lost the connection state(i.e. – the ‘Connected’ box got unchecked) – the solution was to move the VMs to a host that had available switch ports and then set the Nic’s to connected.”

#To find disconnected NICs in Prod Core cluster:" 
$vms = Get-Cluster "Prod Core" | Get-VM 
Get-NetworkAdapter ($vms | where {$_.powerstate -eq “poweredon”}) | Where { $_.connectionstate.connected -eq “$F” } | select parent, connectionstate 
#To repair change all disconnected NICs in a cluster to connected 
$vms = Get-NetworkAdapter ((Get-Cluster "Prod Core HA" | Get-VM) | where {$_.powerstate -eq “poweredon”}) | Where { $_.connectionstate.connected -eq “$F” } | select parent, connectionstate 
foreach ($a in $vms) 
{ 
$vm = get-vm $a.parent 
write-host $a.parent -back green 
if (!(test-connection $vm.name )) 
   { 
   $nics = $vm | get-networkadapter | Where {$_.ConnectionState.Connected -eq $false -and $_.ConnectionState.StartConnected -eq $true} 
   if ($nics -ne $null) 
   { 
           foreach ( $nic in $nics ) 
           { 
                     write-host $vm.Name 
                     write-host $nic 
                        If (test-connection  $vm.Guest.HostName -count 1 -ea 0){write-host "Returning Pings!!"; return}else{write-host "Not Returning Pings"} 
         } 
                $nic | Set-NetworkAdapter -Connected $true 
        } 
  
                        If (test-connection  $vm.Guest.HostName -count 3 -ea 0){write-host "Returning Pings!!"}else{write-host "Not Returning Pings"} 
        } 
} 
Categories: Powershell, Toolbox, VMWare Tags:
  1. No comments yet.
  1. No trackbacks yet.
You must be logged in to post a comment.