3 Replies Latest reply on Jun 12, 2019 1:08 PM by LucD

    Adding more functinality to found script that unmounts LUNs.

    msuvanto_MNIT Lurker

      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

          }

      }

      https://reddit.com/submit/?url=%2f2015%2f09%2f14%2fusing-powercli-to-detach-luns%2f&resubmit=true&title=Using%20PowerCLI%20to%20Detach%20Luns

      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?

       

       

      ?subject=Using%20PowerCLI%20to%20Detach%20Luns&body=%2f2015%2f09%2f14%2fusing-powercli-to-detach-luns%2f

        • 1. Re: Adding more functinality to found script that unmounts LUNs.
          LucD Guru
          User ModeratorsCommunity WarriorsvExpert

          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)

             }

          }

          • 2. Re: Adding more functinality to found script that unmounts LUNs.
            msuvanto_MNIT Lurker

            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.

            • 3. Re: Adding more functinality to found script that unmounts LUNs.
              LucD Guru
              User ModeratorsvExpertCommunity Warriors

              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.