PowerCLI

Expand all | Collapse all

Detach Script

  • 1.  Detach Script

    Posted Oct 21, 2013 07:36 AM

    Hi,

    i need a script that detach luns that contains the name vmax214 from esx servers that contains the name esxdcb.

    thanks for the help.

    Itay



  • 2.  RE: Detach Script

    Posted Oct 21, 2013 07:52 AM

    You can use the Detach-Disk function from my LUN juggling in vSphere 5 post.

    The driving script could look something like this

    foreach($esx in (Get-VMHost | where {$_.Name -match "esxdcb"})){
     
    Get-ScsiLun -LunType disk | Where {$_.CanonicalName -match "vmax214"} | %{
       
    Detach-Disk -VMHost $esx -CanonicalName $_.CanonicalName
      }
    }


  • 3.  RE: Detach Script

    Posted Oct 21, 2013 08:36 AM

    sorry Luc but i forget that i need to unmount before the detach, there is a script that first unmount them and after detach them?



  • 4.  RE: Detach Script

    Posted Oct 21, 2013 10:00 AM

    Are these LUNs all used in datastores ?



  • 5.  RE: Detach Script

    Posted Oct 21, 2013 10:20 AM

    yes, all the luns are used as vmfs.

    i need to unmount the datastores from all esxdcb hosts and after detach them.

    i got another cluster that used these datastores they need to stay there.



  • 6.  RE: Detach Script

    Posted Oct 21, 2013 11:07 AM

    Try something like this

    foreach($esx in (Get-VMHost | where {$_.Name -match "esxdcb"})){
     
    $storSys = Get-View $esx.ExtensionData.ConfigManager.StorageSystem
     
    foreach($ds in (Get-Datastore -RelatedObject $esx | where {$_.Type -eq "VMFS"})){
       
    if($ds.ExtensionData.Info.Vmfs.Extent | where {$_.DiskName -match "vmax214"}){
         
    $storSys.UnmountVmfsVolume($ds.ExtensionData.Info.Vmfs.Uuid)
         
    foreach($extent in $ds.ExtensionData.Info.Vmfs.Extent){
           
    $lun = $storSys.StorageDeviceInfo.ScsiLun |
             
    where {$_.CanonicalName -eq $extent.DiskName}
           
    $storSys.DetachScsiLun($lun.Uuid)
          }
        }
      }
    }


  • 7.  RE: Detach Script

    Posted Oct 21, 2013 11:21 AM

    Hi Luc,

    i run the script but nothing was happen.

    again i will let you the parameters:

    Host name include esxdcb

    datastore name include vmax214



  • 8.  RE: Detach Script

    Posted Oct 21, 2013 11:59 AM

    There was a typo in the code, I just corrected it.

    Can you try again please ?



  • 9.  RE: Detach Script

    Posted Oct 21, 2013 12:08 PM

    still nothing happen without any error



  • 10.  RE: Detach Script

    Posted Oct 21, 2013 01:25 PM

    i don't care if will be 2 scripts, 1 for unmount and 1 for detach



  • 11.  RE: Detach Script

    Posted Oct 21, 2013 05:40 PM

    I just tested the corrected script, and it works correctly for me.

    Are you sure about the hostname (esxdcb) and diskname (vmax214) strings to look for ?

    Perhaps try running this version where I added some extra output to help in debugging.

    foreach($esx in (Get-VMHost | where {$_.Name -match "esxdcb"})){
     
    "Looking at host $($esx.Name)"
     
    $storSys = Get-View $esx.ExtensionData.ConfigManager.StorageSystem
     
    foreach($ds in (Get-Datastore -RelatedObject $esx | where {$_.Type -eq "VMFS"})){
         
    "Looking at $($ds.Name)"
       
    if($ds.ExtensionData.Info.Vmfs.Extent | where {$_.DiskName -match "vmax214"}){
         
    "Found a diskname match"
         
    $storSys.UnmountVmfsVolume($ds.ExtensionData.Info.Vmfs.Uuid)
         
    "Unmounted datastore"
         
    foreach($extent in $ds.ExtensionData.Info.Vmfs.Extent){
           
    $lun = $storSys.StorageDeviceInfo.ScsiLun |
             
    where {$_.CanonicalName -eq $extent.DiskName}
           
    "Detaching LUN with UUID $($lun.Uuid)"
           
    $storSys.DetachScsiLun($lun.Uuid)
          }
        }
      }
    }


  • 12.  RE: Detach Script

    Posted Oct 22, 2013 04:59 AM

    hi Luc,

    this the output from the debug of one esx server:

    Looking at host esxdcb19

    Looking at voldca_vmax214_2

    Looking at voldca_vmax214_3

    Looking at voldca_vmax214_0

    Looking at voldca_vmax214_1

    Looking at voldca_vmax214_4

    Looking at voldca_vmax214_5

    Looking at voldca_vmax214_6

    Looking at voldca_vmax214_7

    Looking at voldca_vmax214_8

    Looking at voldca_vmax214_9

    Looking at voldca_vmax214_10

    Looking at voldca_vmax214_11

    Looking at voldca_vmax214_12

    Looking at voldca_vmax214_13

    Looking at voldca_vmax214_14

    Looking at voldca_vmax214_15

    Looking at voldca_vmax214_16

    Looking at voldca_vmax214_17

    Looking at voldca_vmax214_18

    Looking at voldca_vmax214_19

    after that nothing was happen



  • 13.  RE: Detach Script

    Posted Oct 22, 2013 05:30 AM

    I have the impression the Datastore names contain the string "vmax214", not the disknames (LUN canonicalname).

    Is that correct ?



  • 14.  RE: Detach Script

    Posted Oct 22, 2013 05:57 AM

    yes the datastore names conatin a string with vmax214



  • 15.  RE: Detach Script
    Best Answer

    Posted Oct 22, 2013 06:13 AM

    That changes the algorithm.

    Try like this

    foreach($esx in (Get-VMHost | where {$_.Name -match "esxdcb"})){
     
    $storSys = Get-View $esx.ExtensionData.ConfigManager.StorageSystem
     
    foreach($ds in (Get-Datastore -RelatedObject $esx | where {$_.Type -eq "VMFS" -and $_.Name -match "vmax214"})){
       
    $storSys.UnmountVmfsVolume($ds.ExtensionData.Info.Vmfs.Uuid)
       
    foreach($extent in $ds.ExtensionData.Info.Vmfs.Extent){
         
    $lun = $storSys.StorageDeviceInfo.ScsiLun | where {$_.CanonicalName -eq $extent.DiskName}
         
    $storSys.DetachScsiLun($lun.Uuid)
        }
      }
    }


  • 16.  RE: Detach Script

    Posted Oct 22, 2013 06:46 AM

    Thanks a lot Luc!!