PowerCLI - Configuring Multipathing iSCSI Networks
I’ve been playing in the development hosts quite a bit lately which occasionally leave me having to reset the system back to defaults and it’s no fun to go back through and setup Multipathing for 4 connections everytime. So why not setup a PowerCLI script to do it for me!
Let me forewarn, I’m an intermediate at best and still getting used to PowerShell and PowerCLI. So far so good though.
Let me start by walking through what I have set so far. It’s a script for a single host with 4 NICs on an iSCSI network, using Jumbo Frames, and attaching to 5 iSCSI targets. I left in the commands for vLAN tags and MTU options if you need a vLAN tag or don’t like Jumbo Frames.
Another note: I do not actually change the paths to Round Robin in this script, as I use the EqualLogic MEM to take care of the multipathing for me.
Feel free to download it here in a text format (change extension from txt to ps1) and modify it as necessary: Download Link Here
#==========================================================================
# NAME: VMware Multipathing Script
# AUTHOR: Kyle Ruddy
# DATE : 8/22/2011
# COMMENT: Works on 4 NIC multipathing setups
#==========================================================================
#Global Variables
$HostName = '192.168.10.5'
#NICs assigned to be assigned to the iSCSI switch
$FirstNic = 'vmnic0'
$SecondNic = 'vmnic1'
$ThirdNic = 'vmnic2'
$FourNic = 'vmnic3'
#vSwitch Name & MTU Setting
$SwitchName = 'iSCSI Switch'
$mtu = '9000'
#Virtual Port Group Names
$vPG1 = 'iSCSI1'
$vPG2 = 'iSCSI2'
$vPG3 = 'iSCSI3'
$vPG4 = 'iSCSI4'
#VLAN ID - 0 = none, 4095 = all
$vlanID = '0'
#vKernel Numbers
$vmk1number = 'vmk1'
$vmk2number = 'vmk2'
$vmk3number = 'vmk3'
$vmk4number = 'vmk4'
#vKernel IP Addresses on the iSCSI network
$vmkIP1 = '192.168.1.40'
$vmkIP2 = '192.168.1.41'
$vmkIP3 = '192.168.1.42'
$vmkIP4 = '192.168.1.43'
$Subnet = '255.255.255.0'
#iSCSI Target IPs
$Target1 = '192.168.1.65'
$Target2 = '192.168.1.50'
$Target3 = '192.168.1.5'
$Target4 = '192.168.1.6'
$Target5 = '192.168.1.7'
#End Global Variables
#==========================================================================
#Connect to ESXi Host
Connect-VIServer $HostName
#Create new vSwitch on the given host with the given NICs at the given MTU value
$iscsiswitch = New-VirtualSwitch -VMHost $HostName -Name $SwitchName -Nic $FirstNic,$SecondNic,$ThirdNic,$FourNic -Mtu $mtu -NumPorts '120'
#Create new iSCSI virtual port groups
New-VMHostNetworkAdapter -VMHost $HostName -PortGroup $vPG1 -VirtualSwitch $iscsiswitch -IP $vmkIP1 -SubnetMask $Subnet -Mtu $mtu
New-VMHostNetworkAdapter -VMHost $HostName -PortGroup $vPG2 -VirtualSwitch $iscsiswitch -IP $vmkIP2 -SubnetMask $Subnet -Mtu $mtu
New-VMHostNetworkAdapter -VMHost $HostName -PortGroup $vPG3 -VirtualSwitch $iscsiswitch -IP $vmkIP3 -SubnetMask $Subnet -Mtu $mtu
New-VMHostNetworkAdapter -VMHost $HostName -PortGroup $vPG4 -VirtualSwitch $iscsiswitch -IP $vmkIP4 -SubnetMask $Subnet -Mtu $mtu
#Sets the vLAN for the iSCSI Port Groups
Get-VirtualPortGroup -VMHost $HostName -VirtualSwitch $iscsiswitch -Name $vPG1 | Set-VirtualPortGroup -VLanId $vlanID
Get-VirtualPortGroup -VMHost $HostName -VirtualSwitch $iscsiswitch -Name $vPG2 | Set-VirtualPortGroup -VLanId $vlanID
Get-VirtualPortGroup -VMHost $HostName -VirtualSwitch $iscsiswitch -Name $vPG3 | Set-VirtualPortGroup -VLanId $vlanID
Get-VirtualPortGroup -VMHost $HostName -VirtualSwitch $iscsiswitch -Name $vPG4 | Set-VirtualPortGroup -VLanId $vlanID
#Sets the NIC Teaming Policy to only have one NIC active per iSCSI Port Group
Get-VirtualPortGroup -VMHost $HostName -VirtualSwitch $iscsiswitch -Name $vPG1 | Get-NicTeamingPolicy | Set-NicTeamingPolicy -MakeNicActive $FirstNic -MakeNicUnused $SecondNic,$ThirdNic,$FourNic
Get-VirtualPortGroup -VMHost $HostName -VirtualSwitch $iscsiswitch -Name $vPG2 | Get-NicTeamingPolicy | Set-NicTeamingPolicy -MakeNicActive $SecondNic -MakeNicUnused $FirstNic,$ThirdNic,$FourNic
Get-VirtualPortGroup -VMHost $HostName -VirtualSwitch $iscsiswitch -Name $vPG3 | Get-NicTeamingPolicy | Set-NicTeamingPolicy -MakeNicActive $ThirdNic -MakeNicUnused $FirstNic,$SecondNic,$FourNic
Get-VirtualPortGroup -VMHost $HostName -VirtualSwitch $iscsiswitch -Name $vPG4 | Get-NicTeamingPolicy | Set-NicTeamingPolicy -MakeNicActive $FourNic -MakeNicUnused $FirstNic,$SecondNic,$ThirdNic
#Turns on the iSCSI Initiator
Get-VMHostStorage -VMHost $HostName | Set-VMHostStorage -SoftwareIScsiEnabled $True
#Reads out the HBA number for the iSCSI Software Adapter
$HBANumber = Get-VMHostHba -VMHost $HostName -Type iSCSI | %{$_.Device}
#Sets up PowerCLI to be able to access esxcli commands
$esxcli = Get-EsxCli
#Binds VMKernel ports to the iSCSI Software Adapter HBA
$esxcli.swiscsi.nic.add($HBANumber,$vmk1number)
$esxcli.swiscsi.nic.add($HBANumber,$vmk2number)
$esxcli.swiscsi.nic.add($HBANumber,$vmk3number)
$esxcli.swiscsi.nic.add($HBANumber,$vmk4number)
#Sets the iSCSI Targets
Get-VMHostHba -VMHost $HostName -Type iScsi | New-IScsiHbaTarget -Address $Target1,$Target2,$Target3,$Target4,$Target5
#Performs a Rescan on all HBAs and then a Rescan for VMFS volumes
Get-VMHostStorage -VMHost $HostName -RescanAllHba
Get-VMHostStorage -VMHost $HostName -RescanVmfs
#Disconnect from Host
Disconnect-VIServer