4 Replies Latest reply on Dec 6, 2012 8:53 PM by jaydo123

    help with script to detach LUN's

    jaydo123 Enthusiast

      Hi We are in the process of migrating to new storage and I need help to write a script that will go and remove LUNs from multiple esxi 5 host. I have come across these function which will allow me to detach the LUN's

       

      http://communities.vmware.com/docs/DOC-18008

       

      the help that I need is to write a script using these functions and be able to enter in a cluster name get hosts and  the attached datastores and if they are named decom detach them from all the host in the cluster

       

      I was thinking along these lines

       

      Connect-VIServer xxxxxxx

       

      $clusterName = Read-host "enter Cluster name"

      $cluster = Get-Cluster -Name $clusterName

       

      $cluster | get-vmhost | Get-Datastore | where { %_.name -eq 'decom*'} | detach-datastore

       

      any help would be appreciated

       

       

       

       

        • 1. Re: help with script to detach LUN's
          LucD Guru
          vExpertUser ModeratorsCommunity Warriors

          You should first check if the datastore is not in use.

          See my Test if the datastore can be unmounted.

          If the test is successful you can unmount the datastore and detach the LUN.

          See also my LUN juggling in vSphere 5

          • 2. Re: help with script to detach LUN's
            LucD Guru
            Community WarriorsUser ModeratorsvExpert

            Your Where-clause should be changed like this

            $cluster | get-vmhost | Get-Datastore | where { %_.name -match '^decom'} | detach-datastore

            With the -match operator you can use a RegEx expression.

            The ^ indicates the beginning of the string and then you specify the string that should appear at the beginning.

            • 3. Re: help with script to detach LUN's
              jaydo123 Enthusiast

              thanks Luc

               

              one more question how would I get the devices name  for the RDM's from the identifier?

              • 4. Re: help with script to detach LUN's
                jaydo123 Enthusiast

                This is what we did to decomission the LUN which had been renamed with a decom at the begining

                 

                 

                $ClusterName = "XXXX"
                $ViServer = "XXXXXXXXX"
                Write-Host "Connecting to $ViServer"
                Connect-VIServer -Server $ViServer
                Write-Host "Fetching cluster hosts"
                $ClusterHosts = Get-Cluster -Name $ClusterName | Get-VMHost
                ForEach ($esx in $ClusterHosts) {
                Write-Host "Checking for decommissioned LUNs on $esx"
                $HostView = Get-View $esx | Select Config
                $StorSys  = Get-View $esx.ExtensionData.ConfigManager.StorageSystem
                $LunsFound = 0
                ForEach ($ScsiLun in ($HostView.Config.StorageDevice.ScsiLun | Where-Object {$_.DisplayName -match '^decom_'})) {
                $LunName = $ScsiLun.DisplayName
                $LunUuid = $ScsiLun.Uuid
                Write-Output "  Detaching LUN $LunName"
                $StorSys.DetachScsiLun($LunUuid)
                $LunsFound++
                }
                If ($LunsFound -eq 0) {
                Write-Host "  No decommissioned LUN's found"
                }
                }
                Write-Host "Disconnecting from $ViServer"
                Disconnect-VIServer -Server $ViServer -Confirm:$False