Automation

 View Only
Expand all | Collapse all

Unmount datastores from all hosts

  • 1.  Unmount datastores from all hosts

    Posted Jun 22, 2017 03:15 AM

    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?



  • 2.  RE: Unmount datastores from all hosts

    Posted Jun 22, 2017 04:48 AM

    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)

    }



  • 3.  RE: Unmount datastores from all hosts

    Posted Jun 23, 2017 01:35 AM

    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...



  • 4.  RE: Unmount datastores from all hosts

    Posted Jun 23, 2017 05:59 AM

    My bad, forgot the Key property at the end.

    Updated the code above.



  • 5.  RE: Unmount datastores from all hosts

    Posted Jun 23, 2017 12:44 PM

    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



  • 6.  RE: Unmount datastores from all hosts

    Posted Jun 23, 2017 06:24 PM

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



  • 7.  RE: Unmount datastores from all hosts

    Posted Jun 23, 2017 07:53 PM

    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?



  • 8.  RE: Unmount datastores from all hosts
    Best Answer

    Posted Jun 23, 2017 08:45 PM

    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)

    }



  • 9.  RE: Unmount datastores from all hosts

    Posted Oct 23, 2017 09:51 PM

    OK thanks again.



  • 10.  RE: Unmount datastores from all hosts

    Posted Feb 19, 2019 02:50 PM

    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.



  • 11.  RE: Unmount datastores from all hosts

    Posted Feb 19, 2019 03:07 PM

    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



  • 12.  RE: Unmount datastores from all hosts

    Posted Nov 14, 2020 12:04 PM

    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



  • 13.  RE: Unmount datastores from all hosts

    Posted Apr 10, 2021 11:27 AM

    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



  • 14.  RE: Unmount datastores from all hosts

    Posted Apr 10, 2021 03:09 PM

    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.



  • 15.  RE: Unmount datastores from all hosts

    Posted Apr 10, 2021 03:46 PM

    $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..



  • 16.  RE: Unmount datastores from all hosts

    Posted Apr 10, 2021 03:48 PM

    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.



  • 17.  RE: Unmount datastores from all hosts

    Posted Apr 10, 2021 03:51 PM

    Lucd,

    Could you please correct me here



  • 18.  RE: Unmount datastores from all hosts

    Posted May 17, 2021 09:25 PM

      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