Monday, October 12, 2015

IP Hashing on your clusters and hosts

Many a time i had this issue (blame me ) where i enable ip hashing on the vswitch and i forget to do the same on the portgroups or miss one of the many portgroups of the vswitch which will ultimately make me and my networking guys suffer since now we have to go keep digging from both ends, from both networking and vmware end.
You may checkout the github page where i keep such things to get them updated as and when i get time.
I now had some time and here are few lines to help me kick this problem in the dirt for good.

So here is a way to set the nicteamingpolicy for the vswitchs

get-cluster $cluster | get-vmhost | get-virtualswitch -Name $vss1,$vss2 | Get-NicTeamingPolicy | Set-NicTeamingPolicy -LoadBalancingPolicy LoadBalanceIP

obviously all those words with the $ sign are the variables.
Now how should i check this then? well it is pretty simple and straight forward

get-cluster $cluster | get-vmhost | get-virtualswitch -Name $vss1,$vss2 | Get-NicTeamingPolicy | select VirtualSwitch, LoadBalancingPolicy

Earlier i was saying about the portgroups and how me missing it on them was always a messy headache (headaches are always kinda messy). So let us set the values there too

get-cluster $cluster | get-vmhost | get-virtualswitch -Name $vss1,$vss2 | get-virtualportgroup -Name * | Get-NicTeamingPolicy | Set-NicTeamingPolicy -LoadBalancingPolicy LoadBalanceIP

Let us now check the loadbalancing on these portgroups
get-cluster $cluster | get-vmhost | get-virtualswitch -Name $vss1,$vss2 | get-virtualportgroup -Name * | Get-NicTeamingPolicy | select VirtualPortGroup, LoadBalancingPolicy


Here is the complete script.

<#
.SYNOPSIS
   This will set the ip hashing on the mentioned vswtchs.
.DESCRIPTION
   You have to mention the name of the cluster and vswitchs for it to change the load balancing to ip hash.
.NOTES
    File Name      : IPhash.ps1
    Author         : gajendra d ambi
    Date           : October 2015
    Prerequisite   : PowerShell V3, powercli 5+ over Vista and upper.
    Copyright      - None
.LINK
    Script posted over: github.com/gajuambi/vmware
   
#>

#Start of script
#Variables
$vc=read-host "target vcenter address?"
$user=read-host "username of the vcenter?"
$pass=read-host "password of the vcenter?"
$cluster=read-host "name of the cluster?"
$vss1=read-host "name of the 1st vswitch?"
$vss2=read-host "name of the 2nd vswitch?"

#connect to the vcenter
connect-viserver $vc -user root -Password VMwar3!!

#set nicteamingpolicy for the vswitchs to ip hash
get-cluster $cluster | get-vmhost | get-virtualswitch -Name $vss1,$vss2 | Get-NicTeamingPolicy | Set-NicTeamingPolicy -LoadBalancingPolicy LoadBalanceIP

#get nicteamingpolicy for the vswitchs
get-cluster $cluster | get-vmhost | get-virtualswitch -Name $vss1,$vss2 | Get-NicTeamingPolicy | select VirtualSwitch, LoadBalancingPolicy

#set nicteamingpolicy for the portgroups to ip hash
get-cluster $cluster | get-vmhost | get-virtualswitch -Name $vss1,$vss2 | get-virtualportgroup -Name * | Get-NicTeamingPolicy | Set-NicTeamingPolicy -LoadBalancingPolicy LoadBalanceIP

#get nicteamingpolicy for the portgroups
get-cluster $cluster | get-vmhost | get-virtualswitch -Name $vss1,$vss2 | get-virtualportgroup -Name * | Get-NicTeamingPolicy | select VirtualPortGroup, LoadBalancingPolicy

#End of script