Datastore Mount/Unmount Detach/Attach functions

Datastore Mount/Unmount Detach/Attach functions

A set of functions to allow you to unmount/mount and detach/attach datastores from multiple hosts.

List all datastores and their mount/detach status by passing your datastore object(s) into the Get-DatastoreMountInfo advanced function as shown below:


To unmount a datastore pass your datastore object(s) to the Unmount-Datastore advanced function as seen below, this will unmount the datastore from all attached hosts:


Once unmounted you will then be able to detach the datastore from the hosts using the Detach-Datastore cmdlet as shown below:



Nice functions, come in very handy.

Thanks Luc

Is there a way to detach by the NAA number if a vmfs hadn't been created yet?

Have a look at LUN juggling in vSphere 5

This is very helpful - thank you.

I have unmounted and detached the datastore and confirmed that they display as dismounted and detached ( via Get-Datastore storename | Get-DataStoreMountInfo).

When i remove the assignment from my SAN (HP P4000) we still get alerts along the line

[VMware vCenter - Alarm alarm.StorageConnectivityAlarm] Alarm 'Cannot connect to storage' on changed from Red to Green.

And in vSphere client they show with the red alarm.

I tried rescanning first, but, same effect.

Is this the expected outcome - i was hoping for a "quieter" disconnect.  Does this happen to anyone else?

I tried the script and got a problem/interrogation : only the first lun (extent) of a DataStore is checked by Get-DataStoreMountInfo and detached by Detach-Datastore :

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

By using something like

     $hostviewDSDiskNames = $ds | %{ $_.ExtensionData.Info.vmfs.extent | %{$_.DiskName} }

instead and modifying the equality test for devices :

     #if ($device.canonicalName -eq $hostviewDSDiskName) {    

to something like :

     if ($hostviewDSDiskNames -contains $device.canonicalName) {

ie. testing if the current device is one of the extents forming the DataStore, shouldn't it work better ?

If having serveral LUN in a DS, may it explain  getting StorageConnectivityAlarm ?

Very handy.

Thanks for sharing !

Love these functions but when running the Unmount I get the following errors -

Exception calling "UnmountVmfsVolume" with "1" argument(s): "Cannot complete

the operation due to an incorrect request to the server."

At C:\Users\Chris\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1:

137 char:6





    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

    + FullyQualifiedErrorId : VimException

I had this code in my Powershell Profile and also in a PS1 file alone with the same result.


nice function..very helpful while performing any storage maintenance..I need few more work around on this.. Is it possible to skip unmount/detach the lun where vms running cluster..For example..

if  a single lun is mapped to three clusters in a data center  but In only one cluster vms are running and rest of two clusters there is only if I execute the above function it should skip the cluster where vms running and unmount rest of two clusters..

Hi Very Nice

Is there a way to unmounts just 1 host?  I have the datastore shared by many hosts and only want to remove it from a select few not all of them that are connected to it.

Below are best practices to unmount datastore from vCenter.

Below checks should be considered.

-No VM resides on the datastore

-The datastore is not part of Datastore cluster

-The datastore is not managed by storage DRS

-SIOC is disabled for this datastore

-The datastore is not used for vSphere HA heartbeat.

It seems, Unmount API itself does not do these checks, We need to explicitly handle these checks in script.

From ESXi host

- Only check feasible is : -No VM resides on the datastore

-Remaining checks are not easy to code if we want to unmount from ESXI directly. The reason is ESXi can not know weather SIOC/SDRS etc are enabled.

-If you have to unmount from ESXi, you first should check either manaully or pro-grammatically  from VC  & if everything is OK, finally unmount/detach the datastore from ESXi

I went through this script, this script will work fine BUT there is NO any aforementioned checks. Hence please be cautious while using this script.  If you do not consider these check & run this script on datacenter where multiple hosts/datastores/datastore clusters are there, it may lead to some implications.

To answer your question: Same script work even to unmount datastore from ESXi.

Steps: Save this script as .ps1 (powershell) >>add the datastore name to be unmounted in script>>run this script from powershell (make sure POwerCLI is also installed in the same system)>>it will ask you for credential>>add your ESXi credentials>> it should work to unmount from your host.

I re-iterate, plz make sure you consider all checks I mentioned above.

Hello, habs3-

I replied to your thread at about acting on datastores/LUNs for a single host (instead of all hosts associated with the datastore/LUN).  I included in said thread an updated version of the functions that Alan posted on this page.  Let us know if those functions serve your need.  If so, maybe we'll attach the updated functions at this doc page, too.

Can you please advice me how to use this function i tired but not able to use

PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> C:\Users\murti\Desktop\DatastoreFunctions.ps1

PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> Get-DatastoreMountInfo

Get-DatastoreMountInfo : The term 'Get-DatastoreMountInfo' is not recognized as the name of a cmdlet, function, script file, or operable program.

Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

At line:1 char:1

+ Get-DatastoreMountInfo

+ ~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : ObjectNotFound: (Get-DatastoreMountInfo:String) [], CommandNotFoundException

    + FullyQualifiedErrorId : CommandNotFoundException

1. You will have to execute this script using Powershell. (Internally PowerCLI will come in picture ). 

2. You need to call appropriate function in the script (You can find at the end of the script there are some calls made .

     Ex.  Get-Datastore IX2ISCSI01(Pass you datastore name here) | Unmount-Datastore     (Remove the comments)

3. Follow below best practices before going forward.

Below are best practices to unmount datastore from vCenter.

Below checks should be considered.

-No VM resides on the datastore

-The datastore is not part of Datastore cluster

-The datastore is not managed by storage DRS

-SIOC is disabled for this datastore

-The datastore is not used for vSphere HA heartbeat.

This is great. Automated 70 operations with this.. and another 500 to go tomorrow.

It could do with a -RunAsync option.

Does not work againts vSphere 7 for some reason.




Version history
Revision #:
1 of 1
Last update:
‎01-11-2012 09:47 AM
Updated by: