PowerCLI: Easy iSCSI Send Target setup


In January this year I created a post about Easy NFS datastore setup with PowerCLI. In this post I showed how you can use a reference host to copy all the NFS share configurations to the new host. In this post I will show you how to do the exact same thing only for iSCSI Send targets. I finally find some time to write this post which I promised to write in part 2 of my PowerCLI and iSCSI series.

The following script will check the $REFHOST, in my case esx2.ict-freak.local for all the iSCSI Send targets configured on that host. After that the script will check if all the iSCSI Send targets exists on the $NEWHOST. If this is not the case the script will add the missing Send Targets.

$REFHOST = Get-VMHost "esx2.ict-freak.local"
$NEWHOST = Get-VMHost "esx1.ict-freak.local"

$REFHBA = Get-VMHostHba -VMHost $REFHOST -Type iScsi | Where {$_.Model -eq "iSCSI Software Adapter"}
foreach($target in (Get-IScsiHbaTarget -IScsiHba $REFHBA -Type Send)){
    $target = $target.Address
        $NEWHBA = Get-VMHostHba -VMHost $NEWHOST -Type iScsi | Where {$_.Model -eq "iSCSI Software Adapter"}
        If ((Get-IScsiHbaTarget -IScsiHba $NEWHBA -Type Send | Where {$_.Address -eq $target} -ErrorAction SilentlyContinue )-eq $null){
            Write-Host "Target $($target) doesn't exist on $($NEWHOST)" -fore Red
            New-IScsiHbaTarget -IScsiHba $NEWHBA -Address $target | Out-Null
        }        
}

But there is more..

You can also let the script decide which host is the $REFHOST. In this case you will need to change the $Cluster variable and the $NEWHOST variable. This scenario can be used in a post deployment script.

$Cluster = <ClusterName>
$NEWHOST = Get-VMHost "esx2.ict-freak.local"
$REFHOST = (Get-Cluster $cluster | Get-VMHost | Where {$_.Name -ne $vmHost} | Sort Name | Select -First 1).Name


$REFHBA = Get-VMHostHba -VMHost $REFHOST -Type iScsi | Where {$_.Model -eq "iSCSI Software Adapter"}
foreach($target in (Get-IScsiHbaTarget -IScsiHba $REFHBA -Type Send)){
    $target = $target.Address
        $NEWHBA = Get-VMHostHba -VMHost $NEWHOST -Type iScsi | Where {$_.Model -eq "iSCSI Software Adapter"}
        If ((Get-IScsiHbaTarget -IScsiHba $NEWHBA -Type Send | Where {$_.Address -eq $target} -ErrorAction SilentlyContinue )-eq $null){
            Write-Host "Target $($target) doesn't exist on $($NEWHOST)" -fore Red
            New-IScsiHbaTarget -IScsiHba $NEWHBA -Address $target | Out-Null
        }        
}

But what if you want to use one $REFHOST and check all the other hosts inside a cluster. This can be done with the following script. Just change the $Cluster and $REFHOST variables.

$Cluster = <ClusterName>
$REFHOST = Get-VMHost "esx2.ict-freak.local"
foreach($NEWHOST in (Get-Cluster $cluster | Get-VMHost | Where {$_.Name -ne $REFHOST} | Sort Name)){
    $REFHBA = Get-VMHostHba -VMHost $REFHOST -Type iScsi | Where {$_.Model -eq "iSCSI Software Adapter"}
    foreach($target in (Get-IScsiHbaTarget -IScsiHba $REFHBA -Type Send)){
        $target = $target.Address
            $NEWHBA = Get-VMHostHba -VMHost $NEWHOST -Type iScsi | Where {$_.Model -eq "iSCSI Software Adapter"}
            If ((Get-IScsiHbaTarget -IScsiHba $NEWHBA -Type Send | Where {$_.Address -eq $target} -ErrorAction SilentlyContinue )-eq $null){
                Write-Host "Target $($target) doesn't exist on $($NEWHOST)" -fore Red
                New-IScsiHbaTarget -IScsiHba $NEWHBA -Address $target | Out-Null
            }        
    }
}    

Hope you like the scripts and please be sure to test this scripts first in a lab environment before you use it in production.

Advertisements

One thought on “PowerCLI: Easy iSCSI Send Target setup

  1. Pingback: Gestion des Storages iSCSI sous ESXi » vg5000.org

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s