Home > PowerCLI, Powershell, Scripting, VMware > VMware ESX Multipath scripts

VMware ESX Multipath scripts

Sets all devices on all hosts in a Cluster to Round Robin and enables un optimised paths.  

#Variables
#ESX host username
$Username = “root”
#vSphere CLI esxcli.exe path
$esxcli = “C:\Program Files\VMware\VMware vSphere CLI\bin\esxcli.exe”
#Prompt for vCenter Server
$viServer = Read-Host “Enter vCenter server Name: ”
Connect-VIServer $viServer
#Prompt for vCenter Server Cluster
$viCluster = Read-Host “Enter vCenter Cluster: ”

#list of ESX and ESXi host to perform operation against
$vihosts = Get-Cluster $viCluster | Get-vmhost | sort Name | % {$_.Name}

#prompt user for username
$UsernameInput = Read-Host -AsSecureString:$true “Please enter your common username for ESX hosts”

#prompt user for password
$passwordInput = Read-Host -AsSecureString:$true “Please enter your common password for ESX hosts”

#convert secure password to normal string
$Ptr = [System.Runtime.InteropServices.Marshal]::SecureStringToCoTaskMemUnicode($passwordInput)
$Password = [System.Runtime.InteropServices.Marshal]::PtrToStringUni($Ptr)
[System.Runtime.InteropServices.Marshal]::ZeroFreeCoTaskMemUnicode($Ptr)

#convert secure username to normal string
$Usr = [System.Runtime.InteropServices.Marshal]::SecureStringToCoTaskMemUnicode($UsernameInput)
$Username = [System.Runtime.InteropServices.Marshal]::PtrToStringUni($Usr)
[System.Runtime.InteropServices.Marshal]::ZeroFreeCoTaskMemUnicode($Usr)
#loop through array of hosts and set default SATP policy to RoundRobin
for($i = 0; $i -le $vihosts.length -1; $i++) {
write-Host Executing set default PSP for ALUA_CX to RR on $vihosts[$i]
#insert esxcli parameters at the very end of the statement below
& $esxcli –server $vihosts[$i] –username $Username –password $Password nmp satp setdefaultpsp –satp VMW_SATP_ALUA_CX –psp VMW_PSP_RR
}

#loop through array of hosts and set PSP policy to RoundRobin
foreach ($viHost in $vihosts)
{
write-Host Executing set policy RR per device on $viHost
$lunList = get-vmhost $viHost | get-scsilun -CanonicalName “*” | % {$_.CanonicalName}
foreach ($lun in $lunList)
{
write-Host Executing set policy RR per device on $lun
#could not find a way to store the params in a variable you must
#insert esxcli parameters at the very end of the statement below
#write-host “$esxcli –server $viHost –username $Username –password $Password nmp device setpolicy –psp VMW_PSP_RR –device $lun”
& $esxcli –server $viHost –username $Username –password $Password nmp device setpolicy –psp VMW_PSP_RR –device $lun
}
}

#loop through array of hosts and disable the use of non-optimised paths
foreach ($viHost in $vihosts){
write-Host Executing disable unoptimized paths on $viHost
foreach ($lun in $lunList){
#could not find a way to store the params in a variable you must
#insert esxcli parameters at the very end of the statement below
#write-host “$esxcli –server $viHost –username $Username –password $Password nmp roundrobin setconfig –useANO 0 –device $lun”
& $esxcli –server $viHost –username $Username –password $Password nmp roundrobin setconfig –useANO 0 –device $lun
}
}

Advertisements
  1. John Middleton
    December 24, 2010 at 8:01 pm

    How would you go about using PowerCLI to verify that SAN multipathing is working..?
    So far I have:
    Get-VMHost | Get-ScsiLun | select @{N=”Host”;E={$_.VMHost.Name}},canonicalname,MultipathPolicy

    But this returns the Canonical name and I need (in my mind) the Runtime Name..?

    Any thoughts ?

    Thank you, – John M.

  2. December 26, 2010 at 9:12 pm

    Hi John,

    Try this post, it may be what your after.
    http://communities.vmware.com/message/1652007

    Paul

  3. homepage
    January 14, 2013 at 12:41 pm

    I’ve been browsing online more than 4 hours today, yet I never found any interesting article like yours. It’s pretty worth enough
    for me. Personally, if all site owners and bloggers made good content
    as you did, the web will be a lot more useful than
    ever before.

  1. January 5, 2011 at 8:44 pm

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

%d bloggers like this: