Thursday, May 17, 2012

ESXi5 Change Multipath Policy and IOoperationLimit PowerCLI Script

Recently I had a need to set all of my storage LUN's mulitpath policy to RoundRobin and IO Operation Limit to "1" inside my ESXi 5 environment. Setting these manually can take a long time since you have to hit each seperate LUN. I found out a way to make it really easy to deploy it by clusters. Sure, you can script it by vcenters but this time I wanted more control. After scrouring the internet looking for examples I was unable to find any that worked with ESXi5 and execute across multiply LUNs at one time. Therefor, I create my own using the help of the interweb. I hope this powershell script helps you as well.


#Script to configure the LUNs and Multipathing policies for a cluster.
#Author: Scott Sarkan

# Add the base vSphere cmdlets
Add-PSSnapin VMware.VimAutomation.Core

# This script adds some helper functions and sets the appearance.
"C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCliEnvironment.ps1"

#Define some variables
$vcServer = "Vcenterserver"
$psp = "VMW_PSP_RR"
$DiskID = "naa.5000"
$cluster = "VirtualCluster1"

#Change SATP to match your storage array
$satp = "VMW_SATP_DEFAULT_AA"


#Prompt for Credentials
$vcCred = $host.ui.PromptForCredential("VCENTER LOGIN", "Provide VCENTER credentials (administrator privileges)", "", "")
$esxCred = $host.ui.PromptForCredential("ESX HOST LOGIN", "Provide ESX host credentials (probably root)", "root", "")

#Connect to vCenter
Connect-VIServer -Server $vcServer -Credential $vcCred

#Set Roundrobin Policy on specified LUNs and Connect to ESX hosts for IOPS setting
foreach ($esxhost in Get-Cluster $cluster | Get-VMHost) {
write-output $esxhost.Name #host you are connecting to

#Change Multipathing policy to RoundRobin
#Get-VMHost $esxhost |Get-ScsiLun -LunType "disk"|where {$_.MultipathPolicy -ne "RoundRobin"} | where {$_.CanonicalName -match $diskid}|Set-ScsiLun -MultipathPolicy "RoundRobin"

#Connect to vCenter
Connect-VIServer -Server $esxhost -Credential $esxCred

#Connect to ESXcli
$esxcli = Get-ESXcli

#Go through each storage device where the ID begins with naa.5000
$esxcli.storage.nmp.device.list() | where {$_.device -match $Diskid}| %{

#Capture the current configuration of the focused storage device
$configBefore = $esxcli.storage.nmp.psp.roundrobin.deviceconfig.get($_.device)

#Set the IOoperations limit using the currently focused naa ID
$esxcli.storage.nmp.psp.roundrobin.deviceconfig.set(0,$configBefore.device,[long]1,"iops",$false)

#Capture the configuration post the set
$configAfter = $esxcli.storage.nmp.psp.roundrobin.deviceconfig.get($_.device)

#Returns
$configBefore
$configAfter
}
#Change the default PSP for the specified SATP
#$esxcli.nmp.satp.setdefaultpsp($psp,$satp)


}

#Disconnect from ESX hosts
foreach ($esxhost in Get-Cluster $cluster | Get-VMHost) {
Disconnect-VIServer -Server $esxhost.name -Confirm:$false
}

#Disconnect from vCenter
Disconnect-VIServer $vcServer -Confirm:$false

No comments: