Home > EMC, Powershell, Scripting, VMware > vSphere powercli multipath policy script examples – updated

vSphere powercli multipath policy script examples – updated

#Shows the multipath policy for all LUN’s connected to all hosts in Cluster Primary
Get-Cluster Primary | Get-VMHost | Get-ScsiLun -LunType disk

#Shows the multipath policy for all LUN’s connected to host host4
Get-VMHost host4 | Get-ScsiLun -LunType disk

#Sets the multipath policy for all EMC LUN’s (naa.6006*) connected to all hosts in Cluster RV00 to roundrobin
Get-Cluster RV00 | Get-VMHost | Get-ScsiLun -CanonicalName “naa.6006*” | Set-ScsiLun -MultipathPolicy “roundrobin”

#Sets the multipath policy for all EMC LUN’s connected to host host4
Get-VMHost host4 | Get-ScsiLun -CanonicalName “naa.6006*” | Set-ScsiLun -MultipathPolicy “roundrobin”

# Output the CanonicalName for all the devices on Cluster Primary that are EMC type (naa.6006*)
Get-Cluster Primary | Get-VMHost | Get-ScsiLun -CanonicalName “naa.6006*” | fl -show CanonicalName | out-file c:\shared\naa-id.txt

# This is a script I have developed to connect to a Virtual Center server and set the mulitpathing policy for all datastores on all hosts in a Cluster to Round Robin. You will need to install PowerCLI and vSphereCLI (ebsure you check the path to esxcli in the script matches your installation directory), it will prompt you for the vCenter Server IP, credentials, a Cluster name and the root password for the ESX hosts.

#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 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)

#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 enable the use of non-optimised paths
foreach ($viHost in $vihosts){
write-Host Executing use 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 1 –device $lun”
& $esxcli –server $viHost –username $Username –password $Password nmp roundrobin setconfig –useANO 1 –device $lun
}
}

Advertisements
Categories: EMC, Powershell, Scripting, VMware
  1. bar
    July 13, 2012 at 8:09 pm

    These are very useful. Thanks .

  1. December 18, 2010 at 3:06 am
  2. January 2, 2011 at 10:39 pm
  3. January 5, 2011 at 8:44 pm
  4. March 24, 2011 at 11:13 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: