Add iSCSI send targets with vSphere PowerCLI

Here’s a quick how to add iSCSI send targets on all hosts in your VC

Connect-viserver vCenterServerFQDNorIP
$targets = "StorageTargetIP1", "StorageTargetIP2"
$ESXiHosts  = Get-VMHost
foreach ($esx in $ESXiHosts) {
  $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       
     }
  }
}

Now present the LUNs from storage and rescan all HBA to see the new storage on the hosts.

Get-VMHost | Get-VMHostStorage -RescanAllHba -RescanVmfs

Leave a Reply