Hello,
I am using the below two functions that I found elsewhere on the forums for removing a datastore. I run the Unmount-Datastore followed by the Detach-Datastore functions.
However when I run them, the datastore still shows in vCenter as a red circle with line (inactive and inaccessible). The only way I've found to eliminate it is to reboot each host. This seems akin to the All Paths Down (APD) issue, but I thought I am cleanly removing it, as I run these before doing anything on the LUN/SAN side of this.
Can anyone explain how to get these datastores to cleanly disappear on removal?
Thanks!
Function Detach-Datastore { #function based on code found here https://communities.vmware.com/docs/DOC-18008 [CmdletBinding()] Param ( [Parameter(ValueFromPipeline=$true)] $Datastore ) Process { if (-not $Datastore) { Write-Host "No Datastore defined as input" Exit } Foreach ($ds in $Datastore) { $hostviewDSDiskName = $ds.ExtensionData.Info.vmfs.extent[0].Diskname if ($ds.ExtensionData.Host) { $attachedHosts = $ds.ExtensionData.Host Foreach ($VMHost in $attachedHosts) { $hostview = Get-View $VMHost.Key $StorageSys = Get-View $HostView.ConfigManager.StorageSystem $devices = $StorageSys.StorageDeviceInfo.ScsiLun Foreach ($device in $devices) { if ($device.canonicalName -eq $hostviewDSDiskName) { #If the device is attached then detach it (I added this to the function to prevent error messages in vcenter when running the script) if ($device.operationalState[0] -eq "ok") { $LunUUID = $Device.Uuid Write-Host "Detaching LUN $($Device.CanonicalName) from host $($hostview.Name)..." $StorageSys.DetachScsiLun($LunUUID); } #If the device isn't attached then skip it (I added this to the function to prevent error messages in vcenter when running the script) else { Write-Host "LUN $($Device.CanonicalName) is not attached on host $($hostview.Name)..." } } } } } } } } Function Unmount-Datastore { #function based on code found here https://communities.vmware.com/docs/DOC-18008 [CmdletBinding()] Param ( [Parameter(ValueFromPipeline=$true)] $Datastore ) Process { if (-not $Datastore) { Write-Host "No Datastore defined as input" Exit } Foreach ($ds in $Datastore) { $hostviewDSDiskName = $ds.ExtensionData.Info.vmfs.extent[0].Diskname if ($ds.ExtensionData.Host) { $attachedHosts = $ds.ExtensionData.Host Foreach ($VMHost in $attachedHosts) { $hostview = Get-View $VMHost.Key $mounted = $VMHost.MountInfo.Mounted #If the device is mounted then unmount it (I added this to the function to prevent error messages in vcenter when running the script) if ($mounted -eq $true) { $StorageSys = Get-View $HostView.ConfigManager.StorageSystem Write-Host "Unmounting VMFS Datastore $($DS.Name) from host $($hostview.Name)..." $StorageSys.UnmountVmfsVolume($DS.ExtensionData.Info.vmfs.uuid); } #If the device isn't mounted then skip it (I added this to the function to prevent error messages in vcenter when running the script) else { Write-Host "VMFS Datastore $($DS.Name) is already unmounted on host $($hostview.Name)..." } } } } } }