Write-Host "This script will modify the policy of all your shared LUNs on all ESX Servers" -ForegroundColor CyanWrite-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:" $VMHostsforeach ($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 = 0foreach ($lun in $luns){"count : $count""numpath : $numPaths"$lun.CanonicalNameif ($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}}
Thursday, September 10, 2009
Powershell ESX Lun Balance script
Tuesday, February 10, 2009
ESX iscsi enable script using vi-toolkit.
Param ($VMhost = "")I used this for setting the same settings for all my hosts and ensuring that they all are 100% alike.
$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)
}
Friday, October 17, 2008
List vm usage and assosiated datastore
$datastoreExp = @{N="Datastore"; E={ ($_ | get-datastore | select-object -first 1).Name }}$diskSizeExp = @{N="Total Disk"; E={ ($_ | get-harddisk | measure-object -property CapacityKB -sum).Sum }}get-vm | select Name, $datastoreExp, $diskSizeExp | sort -property Datastore,"Total Disk"
Thursday, September 18, 2008
Updating tools on VM's in Vmware
$VmList = Get-VM | Where-Object {$_.Powerstate -eq "PoweredON"} | % { Get-View $_.ID } | where {$_.guest.toolsstatus -notmatch "Ok"} | Select Name
foreach ($vm in $VmList) {
"Updating :" $vm
Update-Tools $vm
}
Tuesday, July 22, 2008
Sending mail from Esx 3.x servers
tar zxfv MIME-Lite-3.01.tar.gzcd MIME-Lite-3.01perl Makefile.PLmake testmake install
Saturday, June 21, 2008
ESX LUN Load balance
# by Allan Christiansen/2008 on esx 3.5.0 Update 1#for PATHS in 2 4 6 8 10 12 14 16doSTPATHS=${PATHS}COUNTER=${RANDOM}while [[ ${COUNTER} -gt ${PATHS} ]];doCOUNT=`expr ${COUNTER} / ${PATHS}`COUNTER=${COUNT}donefor LUN in $(esxcfg-mpath -l | grep "Disk" | grep "has ${STPATHS} paths" | awk '{print $2}')doesxcfg-mpath --lun=${LUN} --path=$(esxcfg-mpath -q --lun=${LUN} | grep FC | awk '{print $4}' | awk '{print NR "S\t " $0}' | grep ${COUNTER}S | awk '{print $2}') --preferredCOUNT=`expr ${COUNTER} + 1`COUNTER=${COUNT}if [[ ${COUNTER} -gt ${STPATHS} ]]thenCOUNTER="1"fidonedone
Tuesday, June 17, 2008
Vmware ESX
I have for some time been using vmware ESX and found that the software from them is perfect for the growing IT environment that exists in a majority of companies. But i also discovered that managing an environment that is growing fast needs a lot of care and strict procedures. e.g. with vmware there is a possibility to move a vm from one host to another host without user interruption. This is sadly only possible if the cdrom drives are not locked to a iso or otherwise occupied.
I found that running this small powershell script helps alot on that.
Get-VM Get-CDDrive ? { $_.ConnectionState.Connected -eq "true" } Set-CDDrive -Connected:$false -Confirm:$false
This also is a great small script to have in mind when evacuating an ESX. there's nothing more annoying than the last vm will not evacuate because of a mounted cdrom drive or a not ended tools install.
BTW. Thanks alot to Microsoft for the powershell. And for vmware for implementing it in such a great fashion.