I have an interesting situation where I have several datastores that are mounted to hosts in different datacenters, but only Datacenter-A is actually running the VMs, Datacenter-B is not executing anything.
I've tried using the ConfigManager.StorageSystem.DetachScsiLun to no avail. What I need to do is remove the LUNs from Datacenter-B without destroying any VM that is running on Datacenter-A. Maybe there's some API call I'm missing, but I can't seem to figure it out using PowerCLI.
You would need to do an unmount and then a detach for the datastore.
You can use my function from Test If The Datastore Can Be Unmounted to check if it can be unmounted.
And then you can use Alan's functions from Datastore Mount/Unmount Detach/Attach functions
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.
[Param(ValueFromPipeline = $true, Mandatory = $true)]
[Param(Mandatory = $false)]
foreach($ds in $Datastore)
$hostviewDSDiskName = $ds.ExtensionData.Info.vmfs.extent.Diskname
$attachedHosts = $ds.ExtensionData.host
foreach($VMHost in $attachedHosts)
$dataCenterObj = get-vmhost -Id ("HostSystem-$($VMHost.key.value)") | Get-Datacenter
[bool]$Proceed = $true
if($dataCenterObj.Name -ne $Datacenter)
$Proceed = $false
$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)..."
So unmount works, but detach doesn't seem to do anything?
What state is the datastore, and the LUN behind the datastore, in after that?
Could that datastore still be in use for something on one of the ESXi nodes on which is visible?
Did you run my function on all the ESXi nodes that have the datastore?