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

3 thoughts on “Powershell – Set storage LUN multipath policy on VMWare Hosts

  1. Juan M.

    This scripts works great if setting them to Round Robin. If you try to set them to FIXED, you get this error:

    Set-ScsiLun : 8/11/2015 9:50:17 AM Set-ScsiLun If the
    MultipathPolicy parameter is set to ‘Fixed’, you must specify the
    PreferredPath parameter.
    At C:\temp\fixed.ps1:13 char:57
    + | where {$_.MultipathPolicy -ne $MultiPathPolicy} | Set-ScsiLun
    -MultipathPo …
    +
    ~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (:) [Set-ScsiLun], InvalidArgum
    ent
    + FullyQualifiedErrorId : Core_SetScsiLun_TryValidateParameterList_Preferr
    edPathNoSpecified,VMware.VimAutomation.ViCore.Cmdlets.Commands.Host.SetScs
    iLun

  2. Hey again Hayes. I did a BUNCH of testing triyng to get around it. If there is a way, I can’t find it. The issue is that the vSphere Client gets an API version from the VMware Server, and then starts the old v2.5 version of the VI Client. I changed quite a few things server-side to try and fool vSphere into using the new API v4, but in doing so broke almost all functionality from the client. With all the config changes I’ve made, I have now lost the ability to use the vSphere Client at all as well as the web client because of the hacks and changes. Not a big deal for me since this isn’t my production machine. I just don’t think there is a way around it. So I’ve been doing what you are, and using the web client to create them, then managing them with the vSphere client. Good enough, and thanks for the tip. 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s