VMware Cloud Community
itayms
Contributor
Contributor
Jump to solution

Detach Script

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

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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)
    }
  }
}


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

View solution in original post

Reply
0 Kudos
15 Replies
LucD
Leadership
Leadership
Jump to solution

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
  }
}


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

Reply
0 Kudos
itayms
Contributor
Contributor
Jump to solution

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?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Are these LUNs all used in datastores ?


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

Reply
0 Kudos
itayms
Contributor
Contributor
Jump to solution

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.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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)
      }
    }
  }
}


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

Reply
0 Kudos
itayms
Contributor
Contributor
Jump to solution

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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Can you try again please ?


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

Reply
0 Kudos
itayms
Contributor
Contributor
Jump to solution

still nothing happen without any error

Reply
0 Kudos
itayms
Contributor
Contributor
Jump to solution

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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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)
      }
    }
  }
}


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

Reply
0 Kudos
itayms
Contributor
Contributor
Jump to solution

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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Is that correct ?


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

Reply
0 Kudos
itayms
Contributor
Contributor
Jump to solution

yes the datastore names conatin a string with vmax214

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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)
    }
  }
}


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

Reply
0 Kudos
itayms
Contributor
Contributor
Jump to solution

Thanks a lot Luc!!

Reply
0 Kudos