Set Multipathing Policy to Round Robin on all LUNs & Hosts by using PowerCLI

While I was preparing for my VCAP5-DCA, I hit today the PowerCLI topic. IMHO the best way to learn scripting is to write an useful script; so you can practice it.

In my last engagement, I have been asked to configure all LUNs under all Hosts to use Round Robin Multipathing Policy. This could be very tiring manual process, however writing a PowerCLI script to achieve it is one time task; afterwards you can use it generally against any future environment.

The Script is as following:

$vc = “Your vCenter FQDN address”
# The coming line assuming that you have already saved your vCenter Credentials into Credentials.xml
$cred = Get-VICredentialStoreItem -Host $vc -File c:\scripts\Credentials.xml
# Connect to your vCenter
$vCenter = Connect-VIServer $vc -user $cred.user -Password $cred.password
# Get full list of all ESXi Hosts available under your vCenter server
$HostsList = Get-VMHost
# Looping through the whole Hosts list
foreach ($Hosts in $HostsList)
{
$VMHost = $Hosts.Name
Write-Host Modifying the Multipathing policy on host $VMHost
# Connect to esxcli commands environment on the specific Host
$esxcli = Get-EsxCli -VMHost $VMHost
# Get a list of all available LUNs, sorting them by device name
$lunList = Get-VMHost $VMHost | Get-ScsiLun -CanonicalName “*” | % {$_.CanonicalName}
# Looping through all available LUNs
foreach ($lun in $lunList)
{
write-Host Executing set policy RR per device on $lun
# Perform the required Multipathing Policy change to Round Robin against the specific LUN
$esxcli.storage.nmp.device.set($null,$lun,”VMW_PSP_RR”)
}
}
# Disconnect from all Hosts & vCenters
Disconnect-VIServer *

Share

Leave a comment

Your email address will not be published. Required fields are marked *