Reply to Message

View discussion in a popup

Replying to:
UberGeek1
Enthusiast
Enthusiast

I was thinking along those lines and shamelessly used your code to perform the detach on every host in the Datacenter-B, but even with no errors returned the datastore did not detach.  That's where my confusion is coming in, so I will review my code but here's what I have.  I added in an optional Datacenter parameter so that I can ensure only hosts in a perticular datacenter are detached instead of everything, that could be interesting to do in production.

function Remove-SANDatastore

{

   [CmdletBinding()]

   param

   (

      [Param(ValueFromPipeline = $true, Mandatory = $true)]

      [VMware.VimAutomation.ViCore.Types.V1.DatastoreManagement.Datastore]$Datastore,

      [Param(Mandatory = $false)]

      [string]$Datacenter

   )

  foreach($ds in $Datastore)

  {

  $hostviewDSDiskName = $ds.ExtensionData.Info.vmfs.extent[0].Diskname

  if($ds.ExtensionData.Host)

  {

  $attachedHosts = $ds.ExtensionData.host

  foreach($VMHost in $attachedHosts)

  {

                                $dataCenterObj = get-vmhost -Id ("HostSystem-$($VMHost.key.value)") | Get-Datacenter

  [bool]$Proceed = $true

  if($Datacenter)

  {

  if($dataCenterObj.Name -ne $Datacenter)

  {

  $Proceed = $false

  }

  }

  if($Proceed)

  {

  $hostView = Get-View $vmHost.Key

  $StorageSys = Get-View $hostView.ConfigManager.StorageSystem

  $devices = $StorageSys.StorageDeviceInfo.ScsiLun

  foreach($device in $devices)

  {

  if($device.canonicalName -eq $hostviewDSDiskName)

  {

  $LunUUID = $Device.Uuid

                                                        Write-Verbose "Detaching LUN $($Device.canonicalName) from host $($hostview.Name)..."

  $StorageSys.DetachScsiLun($LunUUID);

  }

  }

  }

  }

  }

  }

}

Sincerely, Jody L. Whitlock
Reply
0 Kudos