In part 2 of this series I will show how to remove iSCSI targets with PowerCLI.

But first, let’s see which targets are configured on vSphere host esx2.ict-freak.local:

$esx = Get-VMHost "esx2.ict-freak.local"
Get-IScsiHbaTarget -IScsiHba ($esx | Get-VMHostHba -Type iScsi | Where {$_.Model -eq "iSCSI Software Adapter"})

The following targets are configured:

image

In this post I want to show you how to remove the target with PowerCLI. So I opened the vSphere PowerCLI Cmdlets Reference and searched for the Remove-IScsiHbaTarget. On this page you’ll find the following examples:

————– Example 1 ————–

Get-IScsiHbaTarget -Address 10.23.84.73 -Type Send | Remove-IScsiHbaTarget

Retrieves and removes the targets of type Send on the specified address.

————– Example 2 ————–

Remove-IScsiHbaTarget -Target (Get-IScsiHbaTarget -Address 10.23.84.73)

Removes the specified iSCSI HBA targets.

Unfortunately this example doesn’t work because the –Address parameter doesn’t exist in VMware vSphere PowerCLI 4.1 build 264274.

So if you want to remove an iSCSI target, you can use the this script:

$target = "10.1.1.203"
$esx = Get-VMHost "esx2.ict-freak.local"
Get-IScsiHbaTarget -IScsiHba ($esx | Get-VMHostHba -Type iScsi | Where {$_.Model -eq "iSCSI Software Adapter"}) | `
Where {$_.Address -eq $target} | Remove-IScsiHbaTarget -Confirm:$false

Or you can use the following script which will check if the target exists on the vSphere host:

$esx = Get-VMHost "esx2.ict-freak.local"
$target = "10.1.1.203"
$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
    Write-Host "Removing target: $target" -ForegroundColor Yellow
    Get-IScsiHbaTarget -IScsiHba ($esx | Get-VMHostHba -Type iScsi) | `
    Where {$_.Address -eq $target} | Remove-IScsiHbaTarget -Confirm:$false
}
else{
    Write-Host "The target $target doesn't exist on $esx" -ForegroundColor Red
}

The target does exist on vSphere host esx2.ict-freak.local and will be removed:

image

If you want remove the iSCSI target on all your vSphere host, you can use this script:

$target = "10.1.1.203"
Get-IScsiHbaTarget | Where {$_.Address -eq $target} | Remove-IScsiHbaTarget -Confirm:$false

if you want to remove multiple targets you can use the last script of this post:

$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
                Write-Host "Removing target: $target" -ForegroundColor Yellow
                Get-IScsiHbaTarget -IScsiHba ($esx | Get-VMHostHba -Type iScsi) | `
                Where {$_.Address -eq $target} | Remove-IScsiHbaTarget -Confirm:$false
                Write-Host "done... " -ForegroundColor Green
            }
            else{
                Write-Host "The target $target doesn't exist on $esx" -ForegroundColor Red
            }
        }
}

The targets are remove from the vSphere hosts:

image

To rescan the HBA’s on all your vSphere hosts, you can run the following one-liner:

Get-VMHost | Get-VMHostStorage -RescanAllHba -RescanVmfs

This is the end of part 2. In part 3 I will show you how to integrate these scripts into the Virtualization EcoShell with the VMware Communities Powerpack.

3 thoughts on “How to manage iSCSI targets with PowerCLI part 2

Leave a comment

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