VMware Cloud Community
msuvanto_MNIT
Enthusiast
Enthusiast

Adding more functinality to found script that unmounts LUNs.

I found this small and straight forward handy little script by David Stamen (davidstamen.com)
https://davidstamen.com/2015/09/14/using-powercli-to-detach-luns/

# PowerCLI Script for detaching luns from a cluster

# @davidstamen

# http://davidstamen.com

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

$Clustername = "MyCluster"

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($LUNid in $LunIDs)

    {

        Write-Host "Detaching" $LUNid "from" $VMHost -ForegroundColor "Yellow"

        Detach-Disk -VMHost $VMHost -CanonicalName $LUNid

    }

}

This almost does it for me, I still need to connect to every host involved and removed the detached LUNs

Procedure being first SSH to host and then running the CLI commands:

esxcli storage core device detached list

esxcli storage core device detached remove -d naa.6000000001

esxcli storage core device detached remove -d naa.6000000002

and so on.

I cant help to think they ought to be a way to add this to David's existing script. Utilizing the $LunIDs and $VMHost that have already been defined.

All i seem to be finding are standalone scripts and would appear to be way more convoluted than they need to be.

Any suggestions or pointers?

 

0 Kudos
3 Replies
LucD
Leadership
Leadership

With the help of Get-EsxCli you don't even need a SSH session.

Something like this.
Note that if there are other detached LUNs you do not want to remove, you would need to add a Where-clause before the detach loop.

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

$Clustername = "MyCluster"


Get-Cluster -Name $Clustername | Get-VMHost |

ForEach-Object -Process {

   $storSys = Get-View $_.Extensiondata.ConfigManager.StorageSystem

   Get-ScsiLun -VmHost $_ | where {$LunIDs -contains $_.CanonicalName} |

   ForEach-Object -Process {

   $storSys.DetachScsiLun($_.ExtensionData.Uuid)

   }


   $esxcli = Get-EsxCli -VMHost $_ -V2

   $esxcli.storage.core.device.detached.list.Invoke() |

   ForEach-Object -Process {

   $detach = @{

   device = $_.DeviceUID

   }

   $esxcli.storage.core.device.detached.remove.Invoke($detach)

   }

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
msuvanto_MNIT
Enthusiast
Enthusiast

Being a beginner with Powercli I'm having a hard time to follow that.

Function Detatch-Disk is gone now replaced by - what?

I was initially thinking just adding another line to the tune of

$esxcli.storage.core.device.Detached.Remove($LUNid)

adding that to the foreach loop. Would that not work?

I am absolutely sure your method is more elegant I just don't have the skill set to follow it logically.

0 Kudos
LucD
Leadership
Leadership

I'm afraid not, the Detach method doesn't take a LUNId, but a DeviceUid (which we get from listing the detached LUNs).

On a side note, the script uses the V2 version of Get-EsxCli, that means that the methods are called through the Invoke method.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos