Powershell – Set storage LUN multipath policy on VMWare Hosts

Standard

Setting the multipath policy on ESXi hosts can be long process as it must be configured on every LUN on every host. If one has a lot of storage LUNs used by hosts/VMs this can quickly become a huge amount of tedious and repetitive work. However this script solves that problem and will let you change the configuration on all ESXi hosts and LUNs with ease.
This script will configure all fibre channel LUNs on a given ESXi host to use the value “fixed” or “round robin” depending on what you specify. The script can also be set to do this on all hosts in your HA cluster if needed.
The script is fairly small but I have divided it into three sections variables, functions and script main to simplify the structure.

Variables

#VARIABLES
#parameters
$vCenter = "your_vcenter_server"
$ESXi = "ESXi_FQDN" #use if configuring a single host
$clusterName = "Some_Cluster" #use if configuring all the hosts in a cluster
$MultiPathPolicy = "RoundRobin" #usable values are "Fixed" and "RoundRobin"

You need to fill out the parameters section before running the script
$Vcenter should be the name of your vcenter server
$ESXi should be the name of the ESXi host that you want to configure.
$ClusterName should be the name of the cluster you want configure. All LUNs on the hosts on this cluster will be set to the chosen multipath policy.
$MultiPathPolicy should be set to the value you want to configure on the host/cluster.

Functions

1. Function Set-ESXiMultiPathPolicy

function Set-ESXiMultiPathPolicy {
	param (
		$ESXi #FQDN of ESXi host
	)
	get-VMHost "$ESXi" | Get-VMHostHba -Type "FibreChannel" | Get-ScsiLun -LunType "disk" `
	| where {$_.MultipathPolicy -ne $MultiPathPolicy} | Set-ScsiLun -MultipathPolicy $MultiPathPolicy
}

The script only has this one function which takes one parameter $ESXi which is the ESXi host to configure the path policy on. It loops through all the LUNs on the given host and sets the multipath policy to the chosen value if not already configured like this.

Script Main

#SCRIPT MAIN
clear
#Load snap-in and connect to vCenter
if (-not (Get-PSSnapin -Name "VMware.VimAutomation.Core")) {
	Add-PSSnapin VMware.VimAutomation.Core
}
if ($global:DefaultVIServers.Count -lt 1) {
	Connect-VIServer $vCenter
}
#use this line to only configure one host ($ESXi)
Set-ESXiMultiPathPolicy -ESXi $ESXi

#Use this line to configure all the hosts in the cluster $ClusterName
#Get-Cluster $clusterName | Get-VMHost | % { Set-ESXiMultiPathPolicy -ESXi $_.Name}

The script main first checks if the PS Snap-in VMWare.VimAutomation.Core is loaded. If not the snap-in and commands are loaded. The script then connects to vcenter if it is not already connected. the last couple of lines in the script are the actual commands which run the functions and do the work. Only one of the lines should be run (the other must be commented out) depending on whether you want to configure a single host or all the hosts on a cluster.
That’s it. Enjoy!!
I have copied the full script in below.

################################################################################################
##Script:			Set-ESXiMultiPathPolicy.ps1
##
##Description:		Script configures the multipath policy for all LUNs on a given host or 
#+					cluster.
##Created by:		Noam Wajnman
##Creation Date:	February 2, 2013
##Updated:			March 25, 2014
################################################################################################
#FUNCTIONS
function Set-ESXiMultiPathPolicy {
	param (
		$ESXi #FQDN of ESXi host
	)
	get-VMHost "$ESXi" | Get-VMHostHba -Type "FibreChannel" | Get-ScsiLun -LunType "disk" `
	| where {$_.MultipathPolicy -ne $MultiPathPolicy} | Set-ScsiLun -MultipathPolicy $MultiPathPolicy
}
#VARIABLES
#parameters
$vCenter = "your_vcenter_server"
$ESXi = "ESXi_FQDN" #use if configuring a single host
$clusterName = "Some_Cluster" #use if configuring all the hosts in a cluster
$MultiPathPolicy = "RoundRobin" #usable values are "Fixed" and "RoundRobin"
#SCRIPT MAIN
clear
#Load snap-in and connect to vCenter
if (-not (Get-PSSnapin -Name "VMware.VimAutomation.Core")) {
	Add-PSSnapin VMware.VimAutomation.Core
}
if ($global:DefaultVIServers.Count -lt 1) {
	Connect-VIServer $vCenter
}
#use this line to only configure one host ($ESXi)
Set-ESXiMultiPathPolicy -ESXi $ESXi

#Use this line to configure all the hosts in the cluster $ClusterName
#Get-Cluster $clusterName | Get-VMHost | % { Set-ESXiMultiPathPolicy -ESXi $_.Name}
Advertisements