In part 1 of this series I want to show some basic reporting and how you can add a single target and multiple targets to your vSphere hosts. Let’s start with a simple script to report all the targets on all your vSphere hosts:

$esxHosts = Get-VMhost
foreach($esx in $esxhosts){
$hba = $esx | Get-VMHostHba -Type iScsi 
    Write-Host "=========================================="
    Write-Host "iSCSI Targets on $esx"
    Write-Host "=========================================="
    Get-IScsiHbaTarget -IScsiHba $hba -Type Send | Sort Address
    Write-Host " "
}

The following output will be generated:

image

If you want to report just one host you can add the hostname to the Get-VMhost cmdlet. The report created with the script above shows that there is one target missing on esx2.ict-freak.local. Just for one host you can easily add this via the vSphere client. But in case you manage an environment with ten or more vSphere hosts, this could be a hell of a job. So I created a PowerCLI script to make this task a lot easier:

$esxHosts = Get-VMhost
$target = "10.1.1.203"
foreach($esx in $esxhosts){
$hba = $esx | Get-VMHostHba -Type iScsi | Where {$_.Model -eq "iSCSI Software Adapter"}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" -ForegroundColor Yellow
        New-IScsiHbaTarget -IScsiHba $hba -Address $target        
    }
}

The script above will check all the vSphere hosts if the target 10.3.5.203 exists. If the target doesn’t exist the target will be added to the vSphere host. The output of the script will look like this:

image

Now lets take the above script to the next level and check for and or add multiple targets to your vSphere hosts:

$targets = "10.1.1.201","10.1.1.203","10.1.1.204","10.1.1.205","10.1.1.210"
$esxHosts = Get-VMhost
foreach($esx in $esxhosts){
        $hba = $esx | Get-VMHostHba -Type iScsi | Where {$_.Model -eq "iSCSI Software Adapter"}
        foreach($target in $targets){
            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" -ForegroundColor Yellow
                New-IScsiHbaTarget -IScsiHba $hba -Address $target | Out-Null
                Write-Host "done..." -ForegroundColor Green
            }
        }
}

The targets that doesn’t exist on the vSphere hosts were added:

image

You can verify the changes via the first script of this post or via the vSphere client:

image

This is the end of part 1. In part 2 I will show you how to delete targets with the help of PowerCLI

6 thoughts on “How to manage iSCSI targets with PowerCLI part 1

  1. Hi there,

    I want to reference your PowerCLI script for adding iSCSI Targets in an article I’m writing for TechTarget. It basically discusses the merits of Host Profiles Vs PowerCLI – the lack of iSCSI support in the current version of host profiles is major downside, and the fact you need to be in maintenance mode. I want to show how easy it would be do bulk administration/configuration – and adding multiple iSCSI targets seemed to be good example…

    Is that OK with you?

  2. Hi,

    How can we send the output of below to a file pls ?

    $esxHosts = Get-VMhost
    foreach($esx in $esxhosts){
    $hba = $esx | Get-VMHostHba -Type iScsi
    Write-Host “==========================================”
    Write-Host “iSCSI Targets on $esx”
    Write-Host “==========================================”
    Get-IScsiHbaTarget -IScsiHba $hba -Type Send | Sort Address
    Write-Host ” ”
    }

    Thanks

  3. There are 2 issues with this script which can be fixed:
    Issue #1:
    If the variable $hba results in $Null, then no adapter exists- right!? Well, when you execute the “Get-IScsiHbaTarget” command with a null $hba, it returns a massive list of targets, which of course do not exist. You thus need to test for a $Null $hba before proceeding and only continue when $hba is NOT $Null.
    Issue #2:
    The script only reports on “Send” type targets, which is wrong. It should report on ALL targets as, from my testing, we have “Static” targets in our environment which were not being reported by the original script.
    Issue #3:
    Not an issue really, just an improvement. Some hosts have an unconfigured iSCSI adapter thus no targets, so I added an IF to check this and to advise the status of the adapter.
    Thus, incorporating a fix for the 2 issues, here’s the new, improved and tested to be working OK script:
    $esxHosts = Get-VMhost
    foreach($esx in $esxhosts)
    {
    $hba = $esx | Get-VMHostHba -Type iScsi
    if ($hba)
    {
    $biSCSITarget = Get-IScsiHbaTarget -IScsiHba $hba | Sort Address
    Write-Host “==========================================”
    Write-Host “iSCSI Targets on $esx”
    Write-Host “==========================================”
    # Check if adapter present AND targets present as well.
    if ($biSCSITarget)
    {
    # Targets found.
    Write-Host $biSCSITarget -ForegroundColor Green
    }
    Else
    {
    # No targets found.
    Write-Host “Host has an iSCSI adapter but no targets defined.” -ForegroundColor Red
    }
    Write-Host ” ”
    }
    }

  4. I found an issue with how the “Write-Host $biSCSITarget -ForegroundColor Green” line outputs the results of the variable on a single line like this:
    ==========================================
    iSCSI Targets on MyHost.MyDomain
    ==========================================
    Static-172.20.20.80:3260 Static-172.20.20.81:3260
    Considering the “$biSCSITarget” variable is an object with multi-lines, and Write-Host does not work well with it, I changed the line to:
    $c = Out-String -InputObject $biSCSITarget
    Write-Host $c -ForegroundColor Green
    Which now outputs the results nicely as shown here:
    ==========================================
    iSCSI Targets on MyHost.MyDomain
    ==========================================

    Address Port Type
    ——- —- —-
    172.20.20.80 3260 Static
    172.20.20.81 3260 Static

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 )

Connecting to %s

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