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.