Skip to content

How to configure iSCSI targets on ESXi hosts with PowerCLI

The following script will automate the configuration of  iSCSI targets on ESXi hosts. This is helpful especially when deploying multiple ESXi clusters.

When running the script you will need to connect to your vCenter, select the cluster on which you will make the configuration, set the iSCSI Targets IP and the CHAP passwords. If you are not using CHAP authentication you can remove the $chappassword and $mutualchap from parameters and as well from the New-IScsiHbaTarget.

You need to have the iSCSI software enable (see the previous post) before running the script. It will also check if the target already exists and if no it will add it.

In the end it will rescan the HBA and VMFS.

Connect-VIServer $vcenter 
$targetsIP = $iSCSItarget
$Cluster = Get-Cluster $clustername | Get-VMhost
foreach ($ESXiHost in $Cluster) {
$hba = $ESXihost | Get-VMHostHba -Type IScsi | Where {$_.Model -eq "iSCSI Software Adapter"}
foreach ($target in $targetsIP) {
     # 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 $ESXihost" 
     else {
        Write-Host "The target $target doesn't exist on $ESXihost" 
        Write-Host "Creating $target on $host ..." 
       New-IScsiHbaTarget -IScsiHba $hba -Address $target -ChapType Required -ChapName "user" -ChapPassword $chappassword -MutualChapEnabled $true -MutualChapName "user" -MutualChapPassword $mutualchap       
 Get-VMHostStorage -VMHost $ESXiHost -RescanAllHba -RescanVmfs
Published inGeneral

Be First to Comment

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.