How to manage iSCSI targets with PowerCLI part 1


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

Advertisements

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

  1. Pingback: My Week in Geek – February 4, 2011 : My Geek Finds

  2. Mike Laverick

    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?

    Reply
  3. Pingback: PowerCLI: Return the iSCSI Software Adapter « ICT-Freak.nl

  4. dan

    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

    Reply

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