If you’re like me, you have noticed that adding an iSCSI target to many hosts takes a really long time in the vSphere client. You have to navigate to the configuration tab of each host, enter the storage adapters section, select the iSCSI initiator you want to update, go to properties, to dynamic discovery, add the IP address, and finally rescan the HBA to see the new block device(s).
Many of these steps have a lag time of up to 30 seconds. These steps are obviously not difficult, but this time really starts to add up when you’re updating many hosts.
I’ve created the script below to automate this process, and save some time. When running the script, you’ll be prompted for the vCenter server you’d like to connect to, the IP address of the new iSCSI target, and whether or not you’d like to initiate a rescan on each host after adding the new target IP address.
If there is more than one datacenter in the selected vCenter, you’ll have to choose which datacenter you would like to work with. This script assumes that you want to add the target to all hosts in the datacenter, but you could easily take the section of code that selects the datacenter and reuse it to make a cluster selection.
###############################################
# Author: Brad Payne
# Date: 4/5/13
# Purpose: Add a new iSCSI send target to all hosts in a datacenter
###############################################
if( (Get-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue) -eq $null){
Add-PSSnapin VMware.VimAutomation.Core
}
&"C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1"
FUNCTION ADD-ISCSITARGET($VC,$IP,$RESCANHBA){
Connect-VIServer $VC
$DATACENTERS = @(Get-Datacenter )
IF($DATACENTERS.COUNT -gt 1){
Write-Host "THERE ARE MORE THAN ONE DATACENTERS IN THIS VCENTER: "
FOR($i=1; $i -lt $datacenters.COUNT; $i++){
Write-Host $i")" $datacenters[$i].NAME
}
$SELECTION = Read-Host "WHICH ONE WOULD YOU LIKE TO CONFIGURE?"
$SELECTION = $SELECTION-1
$THISDATACENTER = Get-Datacenter $datacenters[$SELECTION]
}
ELSE{
$THISDATACENTER = $DATACENTERS[0]
}
$HOSTS = GET-DATACENTER $THISDATACENTER |Get-VMHost
FOR($I=0; $i -lt $HOSTS.COUNT; $i++){
$TARGETEXISTS = $FALSE
$TARGETIPS = @(Get-VMHost $HOSTS[$I] | Get-VMHostHba *32 | Get-IScsiHbaTarget)
FOREACH($TARGET IN $TARGETIPS){
IF($TARGET.ADDRESS -eq $IP){
$TARGETEXISTS = $true
}
}
IF($TARGETEXISTS -eq $false){
GET-VMHOST $HOSTS[$I] | Get-VMHOSTHBA *32 | New-IScsiHbaTarget -Address $IP -Port 3260
IF($RESCANHBA){
GET-VMHOST $HOSTS[$I] | Get-VMHostStorage -RescanAllHba -RescanVmfs
}
}
}
}
$vcenter = Read-Host "ENTER THE VCENTER YOU'D LIKE TO CONNECT TO"
$ISCSITARGETIP = Read-Host "ENTER THE IP ADDRESS OF THE NEW ISCSI TARGET"
$RESCAN = READ-Host "ADDING AN ISCSI TARGET IP ADDRESS REQUIRES RESCANNING OF THE HOST HBA IN ORDER TO SEE ANY NEW LUNS. WOULD YOU LIKE TO RESCAN FOLLOWING ADDING THE TARGET IP? "
WHILE($RESCAN -ne "Y" -and $RESCAN -ne "N"){
Write-Host "THAT IS AN INVALID RESPONSE."
$RESCAN = Read-Host "WOULD YOU LIKE TO RESCAN FOLLOWING ADDING THE TARGET IP? "
}
IF ($RESCAN = "Y"){
$RESCAN = $true
}
ELSE{
$RESCAN = $false
}
ADD-ISCSITARGET $VCENTER $ISCSITARGETIP $RESCAN





