Using PowerCLI to Detach Luns

Published on September 14, 2015 under VMware · Tagged PowerCLI and Storage · Read in about 1 min (200 words)

I thought I would share this script, its been circulating a bit but I feel it is a nice one to have in your toolkit.

Have you ever had to remove a LUN from a cluster and thought “Do I really need to detach all the LUN’s, before un-mapping from my SAN?”

  1. Unmount Datastore
  2. Detach LUN
  3. Un-map from SAN
  4. Rescan Cluster

Well here is a handy script with output. All you need to do is unmount your datastore(s), and then enter in your naaid of your LUN’s and the cluster you want to remove it from, save it an execute. All the LUN’s will be detached without having to manually go and detach it from every LUN in your cluster.

The code and Github Link can be found below.


# PowerCLI Script for detaching luns from a cluster
# @davidstamen

$LunIDs = "naa.6000000001","naa.600000002"
$Clustername = "MyCluster"

function Detach-Disk {
        [string]$CanonicalName    )

    $storSys = Get-View $VMHost.Extensiondata.ConfigManager.StorageSystem
    $lunUuid = (Get-ScsiLun -VmHost $VMHost | where {$_.CanonicalName -eq $CanonicalName}).ExtensionData.Uuid


$ClusterHosts = Get-Cluster $Clustername | Get-VMHost

Foreach($VMHost in $ClusterHosts)
    Foreach($LUNid in $LunIDs)
        Write-Host "Detaching" $LUNid "from" $VMHost -ForegroundColor "Yellow"
        Detach-Disk -VMHost $VMHost -CanonicalName $LUNid

Share this post
comments powered by Disqus

See Also