Detach LUN by naa ID

The supported way to remove a LUN from VMware is to unmount and detach before removing your zoning.  Problem is the unmount is easy as they provided a GUI option to do that one.  But the detach has to be done on each hosts adapter, there is no easy button.  PowerCLI to the rescue!  This script will run through each host in a specified cluster and detach it.

Connect-VIServer vcenter.domain.com
$LunIDs = “naa.xxxxxxxxxxxxxx”
$Clustername = “clustername”
function Detach-Disk {
param(
[VMware.VimAutomation.ViCore.Impl.V1.Inventory.VMHostImpl]$VMHost,
[string]$CanonicalName)
$storSys=Get-View$VMHost.Extensiondata.ConfigManager.StorageSystem
$lunUuid=(Get-ScsiLun-VmHost $VMHost|where {$_.CanonicalName-eq$CanonicalName}).ExtensionData.Uuid
$storSys.DetachScsiLun($lunUuid)
}
$ClusterHosts = Get-Cluster $Clustername | Get-VMHost
Foreach($VMHost in $ClusterHosts)
{
Foreach($LUNidin$LunIDs)
{
Write-Host”Detaching”$LUNid”from”$VMHost-ForegroundColor “Yellow”
Detach-Disk -VMHost $VMHost -CanonicalName $LUNid
}
}

 

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 )

Google photo

You are commenting using your Google 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.