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, http://vmjunkie.wordpress.com/ for the basics of this script. a little copy paste and some mods.

# Cluster/host lun balance script
# Allan Christiansen http://doitsmarter.blogspot.com/
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"
$lun.CanonicalName
if ($count -ge $numPaths) { $count = 0 }
$paths = Get-ScsiLunPath -ScsiLun $lun
$paths
$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, http://vmjunkie.wordpress.com for the basics of this.

Tuesday, February 10, 2009

ESX iscsi enable script using vi-toolkit.

Below is a script for enabling and rescanning Iscsi target for valid vmfs volumes. I'm using Vi-Toolkit for this.
   
Param ($VMhost = "")
$iscsiHba = "vmhba32"
$iscsiServer = "Iscsi-SAN-IP"
$iscsiPort = 3260
$target = New-Object VMware.Vim.HostInternetScsiHBASendTarget
$target.address = $iscsiServer
$target.port = $iscsiPort
$iscsiauthprop = New-Object VMware.Vim.HostInternetScsiHbaAuthenticationProperties
#$iscsiauthprop.ChapAuthEnabled = "true"
#$iscsiauthprop.ChapName = "Chapuser"
#$iscsiauthprop.ChapSecret = "ChapSecret"
$h = Get-VMHost $VMhost
Foreach ($hostView in ( Get-View -VIObject $h)) {
    $storageSystem = Get-View $hostView.configManager.storageSystem
    # Enable software iSCSI controller
    $storageSystem.UpdateSoftwareInternetScsiEnabled($true)
    # Add iSCSI Server for dynamic discovery
    $storageSystem.AddInternetScsiSendTargets($iscsiHba, $target)
    $storageSystem.UpdateInternetScsiAuthenticationProperties($iscsiHba,$iscsiauthprop)
    # Scan for iSCSI devices
    $storageSystem.RescanHba($iscsiHba)
}

I used this for setting the same settings for all my hosts and ensuring that they all are 100% alike.