Thursday, September 10, 2009

Powershell ESX Lun Balance script

Back again from a long break. I found out that many of the unix based scripts are going to be obsolete within a couple of month. so here is a powershell version of my earlier published script. Thanks to Justin Emerson, for the basics of this script. a little copy paste and some mods.

# Cluster/host lun balance script
# Allan Christiansen
Write-Host "This script will modify the policy of all your shared LUNs on all ESX Servers" -ForegroundColor Cyan
Write-Host "in a Cluster to Fixed and select a preferred path in a round-robin fashion." -ForegroundColor Cyan
if ($args.Length -lt 2) {
Write-Host "usage: -c or -s "
exit 1
} else {
if ($args[0] -eq "-c") { $VMHosts = Get-Cluster $args[1] | Get-VMHost }
if ($args[0] -eq "-s") { $VMHosts = Get-VMHost $args[1] }
Write-Host "Modifiying ESX hosts:" $VMHosts
foreach ($VMHost in $VMHosts)
$luns = $VMHost|get-scsilun -luntype disk| where-object {$_.ConsoleDeviceName -like "/vmfs/devices/disks/vml*"}| Where-object {$_.CanonicalName -notlike "vmhba32*"} |Sort-Object CanonicalName
$firstLUNPaths = Get-ScsiLunPath $luns[0]
$numPaths = $firstLUNPaths.Length
$count = 0
foreach ($lun in $luns)
"count   : $count"
"numpath : $numPaths"
if ($count -ge $numPaths) { $count = 0 }
$paths = Get-ScsiLunPath -ScsiLun $lun
$lun|Set-ScsiLun -MultipathPolicy Fixed -PreferredPath $paths[$count]
$count += 1
# Sleep for some seconds as some arrays dont like doing this too fast. and with running vm's its .....
Start-Sleep -Seconds 3
# Thanks to Justin Emerson, for the basics of this.