12 Replies Latest reply on Jul 31, 2014 3:29 AM by Vineeth

    Unmout Datastore from 1 host

    habs3 Novice



      Trying to figure out how to unmount a datastore with powercli. I have seen a few examples of unmounting from many host and when I tried to use some of the code the variables were null. Any assistance would be great.

        • 1. Re: Unmout Datastore from 1 host
          vThinkBeyondVM Master
          VMware EmployeesvExpert

          The script which is attached here :Datastore Mount/Unmount Detach/Attach functions works even for unmounting datastore from single ESXi host,

          However, it is must to read below best practices before doing so. (At the end, I have given the steps how to unmount from 1 host using this script)

          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>>enter your ESXi credentials>> it should work to unmount from your host.

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

          • 2. Re: Unmout Datastore from 1 host
            habs3 Novice

            Thank you for your response. However if I read the unmounts function correctly it is gogin to get all the hosts that are connected to the datastore and unmounts from those hosts. Unfortunately I have about 10 hosts that a datastore is mounted to and I do not want all the hosts to be unmounted from the datastore that is why I need to get the unmount function to do it from the host and ask for the specific datastore name to unmount. I have been looking at the script you provided a link to try and create a hosts to single datastore solution.

            • 3. Re: Unmout Datastore from 1 host
              vThinkBeyondVM Master
              VMware EmployeesvExpert

              Let me try this script on single ESXi host. I will come back with my experience on the same.

              • 4. Re: Unmout Datastore from 1 host
                mattboren Master

                Hello, habs3-


                We, like you, had need to act on datastores/LUNs at the single-host level instead of using "all hosts with mount".  I updated the functions that vickyvision2020 mentions (from lamw / alanrenouf -- many thanks go to them, of course, for originally providing the functions) to now allow for acting on just select VMHosts.  I have attached a file here with the updated functions.  As is the disclaimer on the original functions, comsumers should test these first to make sure that the functions provide the expected behavior.


                How do these updated functions do for you?

                • 5. Re: Unmout Datastore from 1 host
                  LucD Guru
                  Community WarriorsUser ModeratorsvExpert

                  Great update to the functions by Matt.


                  You might also want to consider using the Get-DatastoreUnmountStatus function to check if the datastore can be unmounted, and if not why.

                  1 person found this helpful
                  • 6. Re: Unmout Datastore from 1 host
                    habs3 Novice

                    Thank you for taking the time to put this together and update the original script. I will try it out in our test env.

                    • 7. Re: Unmout Datastore from 1 host
                      habs3 Novice

                      Thanks Lucd.

                      I did see your script to validate the unmounts. I was planning on using this to make sure all the datastores were able to be unmounted.


                      Great work (as usual)

                      • 8. Re: Unmout Datastore from 1 host
                        habs3 Novice

                        Good morning Matt


                        In looking at the code it appears that this specific line does the unmounts of the datastore.




                        I get that parameter that is being retrieved is the UUID or Cannocial Name of the Lun in the brackets. what paremeter is in $viewstoragesysthishost variable?


                        I am trying to pick this apart to decipher the exact unmounts of the datastore if you did not use variables and had specific data in there so I can alter it for the use I need currently.



                        Thanks again for your assistance this is really great learning opportunity for me.

                        • 9. Re: Unmout Datastore from 1 host
                          mattboren Master

                          Hello, -


                          Yes, that is the actual "business" line for the function Unmount-Datastore.


                          As for the variable $viewStorageSysThisHost, that is the View object representing the ConfigManager.StorageSystem of the current HostSystem from which the given VMFS volume is being dismounted.


                          Though, what is your current need?  If it is to dismount a datastore from a single VMHost, you should be able to use the updated function as-is, passing the proper parameters.  Example:


                          Get-Datastore myOldDatastore0 | Unmount-Datastore -VMHost (Get-VMHost myhost0.dom.com)


                          This will get datastore "myOldDatastore0" and then unmount it on the VMHost specified, "myhost0.dom.com".  I thought that was the scenario that you described that you needed to satisfy.  Is that not the case?


                          And, once you have the function defined in your PowerShell session, you can get further help- and examples for it in the standard PowerShell way:


                          Get-Help -Full Unmount-Datastore

                          • 10. Re: Unmout Datastore from 1 host
                            habs3 Novice

                            Thank you,


                            So the questions are for 2 reasons. I try to pick these type of things apart so I can further learn powershell and to up my powershell scripting abilities. I am not a programmer but a sys admin and do not have the background or experience writing some of the more intricate scripts to do what I need. The other is so I can really create the specific script to do what I need it to do. I was asking for help to unmounts 1 daatastore from 1 host. The scope is a bit more than that.


                            I have a cluster that has 10 hosts. these hosts share datastores with another cluster and this was for a migration. The migration is complete so now I want to unmount about 70 datastores(out of a total 120) from these 10 hosts and was planning something like the following


                            text file of hosts

                            text file of UUIDs of datastores to unmount (and later used to detach)

                            do a get-content of uuid to an array

                            do a get-content of host to an array

                            a foreach HOST

                            foreach UUID

                            unmount UUID from Host

                            log the status of work



                            I will be doing this work on 4 other clusters to complete this work with the storage administrator.


                            So would something like the following line work if I was doing this without variables?


                            (get-vmhost "$VMHOST" | get-view).ConfigManager.StorageSystem.Value.UnmountVMFSVolume $UUID


                            Thanks again for your help. This has helped me a lot.

                            • 11. Re: Unmout Datastore from 1 host
                              mattboren Master

                              Hello, habs3-


                              About your reasons:

                              reason 0)  picking things apart so as to learn further -- great, glad to hear it.

                              reason 1)  I understand your need, I believe:  to unmount 70 datastores from some (not all) of the VMHosts that have these datastores mounted.  That is, unmount these given datastores on 10 VMHosts, and not on the rest of the hosts that have these datastores mounted.


                              If so, then that line that you gave is pretty close to what would be at the heart of the Foreach-Object scriptblock that you would likely write to iterate through the VMHosts and UUIDs.


                              However, you should be able to use the updated Unmount-Datastore function that is in the update function definitions file attached in this thread.  This updated Unmount-Datastore will unmount a datastore from only the VMHost(s) that you specify, if you so choose.  So, while understanding the inner workings of the function is always a great idea, you need not rewrite the function in this case.


                              For example, suppose we have two datastores mounted on each of twelve VMHosts.  To unmount these two datastores on each of three specific VMHosts only, and not unmount them from the other nine VMHosts, you could use Unmount-Datastore like:


                              ## the two datastores that are mounted on all twelve hosts
                              $arrDStores = Get-Datastore myDatastore0,myDatastore1
                              ## the three VMHosts on which to unmount these datastores
                              $arrVMHostsForUnmount = Get-VMHost somehost0.dom.com, somehost5.dom.com, somehost6.dom.com
                              ## unmount the two datastores from the three VMHosts (running in WhatIf mode -- does not actually unmount until you remove the -WhatIf)
                              Unmount-Datastore -WhatIf -VMHost $arrVMHostsForUnmount -Datastore $arrDStores


                              And, using the function (as in this example), you can specify the -WhatIf parameter, so that you can report on what the function would do without actually doing it.


                              So, in your case, you have the datastore names in a data file somewhere (like, a text file as mentioned), use Get-Datastore on those names, and use Get-Cluster <myClusterOfTenHosts> | Get-VMHost to get the VMHosts for the unmount.  Then you would be set to Unmount-Datastore with those two params (and -WhatIf to verify before doing it).  That might look something like:


                              ## the 70 datastores to be unmounted on 10 VMHosts
                              $arrDStores = Get-Datastore -Name (Get-Content c:\temp\datastoreNamesForUnmount.txt)
                              ## the 10 VMHosts on which to unmount these datastores
                              $arrVMHostsForUnmount = Get-Cluster -Name "hba3s cluster Of 10 Hosts" | Get-VMHost
                              ## unmount the 70 datastores from the 12 VMHosts (running in WhatIf mode -- does not actually unmount until you remove the -WhatIf)
                              Unmount-Datastore -WhatIf -VMHost $arrVMHostsForUnmount -Datastore $arrDStores


                              Seem like a way to do it that would fit your needs?  And, to be extra cautious, you could try first with just a single datastore name in that txt file, and with just one VMHost, with -WhatIf, to see the report of what it would do (without doing anything).


                              One other thing:  I added a ConfirmImpact value of "High" to the Unmount-Datastore function, so it will prompt for confirmation before doing any unmounts (unless you include -Confirm:$false in the command line).


                              And, yes, you are welcome.  Glad to try to help.

                              • 12. Re: Unmout Datastore from 1 host
                                Vineeth Hot Shot

                                Hello habs3


                                Are you looking like this..


                                Function Detach-Datastore {   [CmdletBinding()]   Param (   [Parameter(ValueFromPipeline=$true)]   $Datastore,   [Parameter(ValueFromPipeline=$true)]   $VMHost   )   Process {   if (-not $Datastore) {   Write-Host "No Datastore defined as input"   Exit   }   $tgtHosts = $VMHost | %{$_.Name}   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   if($tgtHosts -contains $hostview.Name){   $StorageSys = Get-View $HostView.ConfigManager.StorageSystem   $devices = $StorageSys.StorageDeviceInfo.ScsiLun   Foreach ($device in $devices) {   if ($device.canonicalName -eq $hostviewDSDiskName) {   $LunUUID = $Device.Uuid   Write-Host "Detaching LUN $($Device.CanonicalName) from host $($hostview.Name)..."   $StorageSys.DetachScsiLun($LunUUID);   }   }   }   }   }   }   } } Function Unmount-Datastore {   [CmdletBinding()]   Param (   [Parameter(ValueFromPipeline=$true)]   $Datastore,   [Parameter(ValueFromPipeline=$true)]   $VMHost   )   Process {   if (-not $Datastore) {   Write-Host "No Datastore defined as input"   Exit   }   $tgtHosts = $VMHost | %{$_.Name}   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   if($tgtHosts -contains $hostview.Name){   $StorageSys = Get-View $HostView.ConfigManager.StorageSystem   Write-Host "Unmounting VMFS Datastore $($DS.Name) from host $($hostview.Name)..."   $StorageSys.UnmountVmfsVolume($DS.ExtensionData.Info.vmfs.uuid);   }   }   }   }   } } $esx= Get-Datacenter "testdc" |Get-Cluster "testcluster" |Get-VMHost $ds = Get-Datastore "testdatastore1","testdatastore2" Unmount-Datastore -Datastore $ds -VMHost $esx Detach-Datastore -Datastore $ds -VMHost $esx