PowerCLIStoragevHerseyVMware

Enable Software iSCSI and Add SendTargets with PowerCLI

Another script in my series of PowerCLI scripts to assist with the configuration of a vSphere environment. This PowerCLI script enables the Software iSCSI Adapter and sets the iSCSI SendTargets (Dynamic Discovery) on each host.

Configure $ESXiHost with the FQDNs or IP Addresses of the hosts to be configured and set $targets to the iSCSI SendTargets to configure. When the script is run it will prompt for the ESXi credentials to use, the credentials must be the same on all hosts, and then connects to each host listed in $ESXiHosts.

The script then enables the Software iSCSI Adapter and checks to see if the $targets have already been configured, if not it adds the target.

#FQDNs or IP addresses of ESXi Hosts to Configure
#Enclose each host in quotes and separate with a comma.
#Example: $ESXiHosts = "192.168.1.1","192.168.1.2"
$ESXiHosts = "192.168.1.1", "192.168.1.2"

# Prompt for ESXi Root Credentials
$esxcred = Get-Credential 

#Connect to each host defined in $ESXiHosts
Connect-viserver -Server $ESXiHosts -Credential $ESXiCreds

# Set $targets to the SendTargets you want to add. Enclose each target in quotes and separate with a comma.
# Example: $targets = "192.168.151.10", "192.168.151.11", "192.168.151.12", "192.168.151.13"
$targets = "192.168.151.10", "192.168.151.11"

foreach ($esx in $ESXiHosts) {

  # Enable Software iSCSI Adapter on each host
  Write-Host "Enabling Software iSCSI Adapter on $esx ..."
  Get-VMHostStorage -VMHost $esx | Set-VMHostStorage -SoftwareIScsiEnabled $True 

  # Just a sleep to wait for the adapter to load
  Write-Host "Sleeping for 30 Seconds..." -ForegroundColor Green
  Start-Sleep -Seconds 30
  Write-Host "OK Here we go..." -ForegroundColor Green
  Write-Host "Adding iSCSI SendTargets..." -ForegroundColor Green

  $hba = $esx | Get-VMHostHba -Type iScsi | Where {$_.Model -eq "iSCSI Software Adapter"}

  foreach ($target in $targets) {

     # Check to see if the SendTarget exist, if not add it
     if (Get-IScsiHbaTarget -IScsiHba $hba -Type Send | Where {$_.Address -cmatch $target}) {
        Write-Host "The target $target does exist on $esx" -ForegroundColor Green
     }
     else {
        Write-Host "The target $target doesn't exist on $esx" -ForegroundColor Red
        Write-Host "Creating $target on $esx ..." -ForegroundColor Yellow
        New-IScsiHbaTarget -IScsiHba $hba -Address $target        
     }

  }

}
Write "`n Done - Disconnecting from $ESXiHosts"
Disconnect-VIServer -Server * -Force -Confirm:$false

Write-Host "Done! Now go manually add the iSCSI vmk bindings to the Software iSCSI Adapter and Resan." -ForegroundColor Green

Once the script finishes the vmk binding for software iSCSI must still be configured manually ( until the next script 🙂 ) but still a lot faster than connecting to each host using the vSphere Client to enable Software iSCSI and add the targets. Also as with anything else that is scripted it ensures consistent configuration across each host.

As always comments or recommendations for improvement are always welcome.

2 thoughts on “Enable Software iSCSI and Add SendTargets with PowerCLI

  • Merzavets

    Thank you a lot! Your script saves me more than a hour.

    And there is one small caveat: when the script reaches line 28 it throws an exeption:

    Get-VMHostHba : Cannot process argument transformation on parameter 'VMHost'. Strings as pipeline input are not supported.
    At C:\Users\Serge\Documents\Add-iscsiHBA.ps1:29 char:10
    + $hba = $esx | Get-VMHostHba -Type iScsi | Where {$_.Model -eq "iSCSI Software Adapter ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~

    I’ve modified this line this way:

    $hba = get-vmhost $esx [...]

    and problem disappeared 🙂

    Thank you once again, VHersey!

    Reply
  • Joakim Hellström

    Good script.
    Actuallt, line 29 should be $hba = GetVMHost -VMHost $esx ….

    And you should add “Get-VMHostStorage -VMHost $esx -RescanAllHba” and “get-VMHostStorage -VMHost $server -RescanVmfs” for each host.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

eight − six =