Home > EMC, PowerCLI, Powershell, Scripting, VMware > Scripting esxcli commands

Scripting esxcli commands

Update Dec 2010

Refer to this and this post for example scripts for setting the mutlipath policies of ESX devices (datastores).

Also PowerCLI 4.1 now offers direct experienmental support for esxcli from within PowerCLI.

http://blogs.vmware.com/vipowershell/2010/12/esxcli-now-available-through-powercli.html

—————————

I wanted to script the ESX configuration of multipath policies for specific devices. I found that powercli could do the mulitpath policy as per the following command:

Get-Cluster “clustername” | Get-VMHost | Get-ScsiLun -CanonicalName “naa.6006*” | Set-ScsiLun -MultipathPolicy “roundrobin”

naa.6006 is EMC storage, this site has EMC and HP storage but I was only modifying the EMC path policies.

But the esxcli command to disable unoptimized paths wasn’t available with powercli:

esxcli nmp roundrobin setconfig –useANO 0 –device naa.60060160e8802900e7414c361ba4df11

Using the following powershell script from William Liam as the basis I was able to produce a script which can run multiple esxcli commands against multiple hosts, perfect!

http://www.virtuallyghetto.com/2010/06/esxcli-part3-automating-esxcli-using.html

Example:

$Username = “root”
$esxcli = “C:\Program Files\VMware\VMware vSphere CLI\bin\esxcli.exe”
#$Params = “nmp roundrobin setconfig –useANO 1 –device naa.60060160e880290005445743c3a5df11”

#list of ESX and ESXi host to perform operation against
$vihosts = @(“host1″,”host2″,”host3”)

#prompt user for password
$passwordInput = Read-Host -AsSecureString:$true “Please enter your common password for 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
for($i = 0; $i -le $vihosts.length -1; $i++) {
write-Host Executing set default PSP for ALUA_CX to RR on $vihosts[$i]
#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
$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
for($i = 0; $i -le $vihosts.length -1; $i++) {
write-Host Executing set policy RR per device on $vihosts[$i]
#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
$esxcli –server $vihosts[$i] –username $Username –password $Password nmp device setpolicy –psp VMW_PSP_RR –device naa.60060160e880290005445743c3a5df11
$esxcli –server $vihosts[$i] –username $Username –password $Password nmp device setpolicy –psp VMW_PSP_RR –device naa.60060160e8802900bdb0f3eb16a4df11
$esxcli –server $vihosts[$i] –username $Username –password $Password nmp device setpolicy –psp VMW_PSP_RR –device naa.60060160e8802900c178f2bbc3a5df11
}

#loop through array of hosts
for($i = 0; $i -le $vihosts.length -1; $i++) {
write-Host Executing use unoptimized paths on $vihosts[$i]
#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
$esxcli –server $vihosts[$i] –username $Username –password $Password nmp roundrobin setconfig –useANO 1 –device naa.60060160e880290005445743c3a5df11
$esxcli –server $vihosts[$i] –username $Username –password $Password nmp roundrobin setconfig –useANO 1 –device naa.60060160e8802900bdb0f3eb16a4df11
$esxcli –server $vihosts[$i] –username $Username –password $Password nmp roundrobin setconfig –useANO 1 –device naa.60060160e8802900c178f2bbc3a5df11
}

Advertisements
  1. No comments yet.
  1. January 2, 2011 at 10:39 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: