TheVMinator
Expert
Expert

Unmount datastores from all hosts

Jump to solution

Is there a way to import a list of datastores that need to be unmounted from all hosts that have them mounted, then find each host that has that datstore mounted and unmount the datastore from all hosts?

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

Try like this, that should also work for NFS datastores

$dsName = 'MyDS'

$uArgs = @{

    volumelabel = $dsName

    nopersist = $true

}

Get-VMHost -Datastore $dsName | %{

    $esxcli = Get-EsxCli -VMHost $_ -V2

    $esxcli.storage.filesystem.unmount.Invoke($uArgs)

}


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

View solution in original post

0 Kudos
17 Replies
LucD
Leadership
Leadership

Try something like this

Get-Datastore -Name (Get-Content datastores.txt) | %{

    $esx = Get-View -Id $_.ExtensionData.host[0].Key

    $storSys = Get-View -Id $esx.ConfigManager.StorageSystem

    $storSys.UnmountVmfsVolume($_.ExtensionData.Info.vmfs.uuid)

}


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

TheVMinator
Expert
Expert

OK thanks - it's erroring out though:

Get-View : Cannot bind parameter 'Id'. Cannot convert the

"VMware.Vim.DatastoreHostMount" value of type "VMware.Vim.DatastoreHostMount" to

type "VMware.Vim.ManagedObjectReference".

At C:\scripts\vmware_remove_datastores.ps1:22 char:25

+     $esx = Get-View -Id $_.ExtensionData.host[0]

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

    + CategoryInfo          : InvalidArgument: (:) [Get-View], ParameterBindingExce

   ption

    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,VMware.VimAutomation.V

   iCore.Cmdlets.Commands.DotNetInterop.GetVIView

Any ideas?  I'm sure it is getting the datastore OK and passing it down the pipe but it looks like $_.Extensiondata.host[0] isn't returning a result...

0 Kudos
LucD
Leadership
Leadership

My bad, forgot the Key property at the end.

Updated the code above.


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

glezama
Contributor
Contributor

I tried it. Got the following error:

Exception calling "UnmountVmfsVolume" with "1" argument(s): "The object or item

referred to could not be found."

At C:\scripts\vmware_remove_datastores.ps1:23 char:5

+     $storSys.UnmountVmfsVolume($ds.ExtensionData.Info.vmfs.uuid)

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

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

    + FullyQualifiedErrorId : VimException

0 Kudos
LucD
Leadership
Leadership

That shouldn't be $ds but $_, I updated the code


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

0 Kudos
glezama
Contributor
Contributor

I tracked down the reason it is not working for me. The NFS datastores I am mounting from our net app appliance do not have a value for .ExtensionData.Info.vmfs.uuid. Can anyone shed light on why this is so?

0 Kudos
LucD
Leadership
Leadership

Try like this, that should also work for NFS datastores

$dsName = 'MyDS'

$uArgs = @{

    volumelabel = $dsName

    nopersist = $true

}

Get-VMHost -Datastore $dsName | %{

    $esxcli = Get-EsxCli -VMHost $_ -V2

    $esxcli.storage.filesystem.unmount.Invoke($uArgs)

}


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

View solution in original post

0 Kudos
TheVMinator
Expert
Expert

OK thanks again.

0 Kudos
user146
Contributor
Contributor

Hi LucD,

I have tested the script you provided and it's working, but I don't fully understand each step and I was hoping you may be able to explain its operation?

Thanks.

0 Kudos
LucD
Leadership
Leadership

Sure, the script uses the esxcli command to unmount the datastore.

See for example KB2004605

Normally you give those esxcli commands from an ESXi console, but with PowerCLI you can use the Get-EsxCli cmdlet.

With the V2 option, we can now pass the parameters to method we are calling, with a hash table.

See PowerCLI 6.3 R1: Get-ESXCLI Why the V2?

And with the ForEach loop, we go through all ESXi nodes that have the datatsore mounted.
For each ESXi node we have to do the Get-EsxCli again, but the hash table stays the same for each ESXi node..

I hope this helps a bit


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

0 Kudos
rakaloor
Contributor
Contributor

Hello Lucd,

how do i unmount and detach datastore from esxcli, I need to test it for one esxi host
$esxcli.storage.filesystem.unmount.Invoke
$esxcli.storage.filesystem.mount.Invoke

Could you please guide me

0 Kudos
Raghukalloor
Contributor
Contributor

Hi Lucd,

I have one DS to be unmounted first and later to be mounted from 10 esxi hosts and I have to combine the below commands with this to completely remove it.

unmount
==================
esxcli storage filesystem unmount -l xxxxxxxx;esxcli storage core device set --state=off -d xxxxxxx;esxcli storage core adapter rescan --all;vmkfstools -V

mount
==================
esxcli storage core device set --state=on -d xxxxxxx;esxcli storage core adapter rescan --all;esxcli storage filesystem mount -l xxxxxx;vmkfstools -V

Can you help me in combining it to .ps1 script

0 Kudos
LucD
Leadership
Leadership

Have a look at the Get-EsxCli cmdlet.
Via the returned object you can run all esxcli commands.
See PowerCLI 6.3 R1: Get-ESXCLI Why the V2? for some more background info.


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

0 Kudos
Raghukalloor
Contributor
Contributor

$esxNames = Import-Csv -Path "C:\ISO\hostnames.csv" -UseCulture | Select -ExpandProperty Name
$dsName = "datastore1"
Get-VMHost -Name $esxNames -PipelineVariable esx |
ForEach-Object -Process {
$esxcli = Get-EsxCli -VMHost $_ -V2
$esxcli.storage.filesystem.unmount.Invoke(@{volumelabel=$dsName})
$esxcli.storage.core.device.set.Invoke("deviceid", $null, $false,'off')
}

I am running this and its throwing an error..

0 Kudos
LucD
Leadership
Leadership

You didn't provide a hash table with the arguments on the 2nd call.
You can get a skeleton hash table by using the CreateArgs method.


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

0 Kudos
Raghukalloor
Contributor
Contributor

Lucd,

Could you please correct me here

0 Kudos
Prabhu_Krishna
Contributor
Contributor

@LucD  I am getting the below error though I use -V2 ? Can you please help ?

Message: A specified parameter was not correct: argument[0];
InnerText: argument[0]
At line:12 char:5
+ $esxcli.storage.filesystem.unmount.Invoke($uArgs)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], InvalidArgument
+ FullyQualifiedErrorId : VMware.VimAutomation.ViCore.Types.V1.ErrorHandling.InvalidArgument

0 Kudos