Unmount datastore

Was at VMUGDK the other day listen to Paudie O'Riordan to one of his great session on storage troubleshooting - He was talking about how to remove datastores safely and this script that I've created for our storage team earlier this year came to mind.

The script reli on William Lam's powershell module which can be found here http://blogs.vmware.com/vsphere/2012/01/automating-datastore-storage-device-detachment-in-vsphere-5.html

The script has four options; list, unmount detach and all - The first three is self explanatory and the latter simply does all three, first it unmounts the datastore, then it detaches the datastore and lastly it list the datastore to verify it status.

<#.SYNOPSISRemove_Datastores.ps1.DESCRIPTIONThis script makes it possible to Unmount, detach, list or all of the above for at list of datastore(s).PARAMETER CommandUnmount - Unmounts the datastore(s) on all host(s) where its mountedDetach - Detaches the datastore(s) on all host(s) where its attachedList - List the datastore(s), VMhosts, Lun, mounted and stateAll - First it unmounts the datastore(s), then it detaches the datastore(s) and lastly it lists the datastore(s).PARAMETER vcenterSpecify the vCenter to which you want to connect to, in order to get data from..PARAMETER datastoresSpecify the datastore(s) - Use comma to seperate datastores.If datastore contains spaces - The datastore needs to be inclosed in "".EXAMPLEC:\PS>Remove_Datastores.ps1 list vcenter.local.domain datastore01,datastore02.OUTPUTSDatastore VMHost Lun Mounted State--------- ------ --- ------- -----Datastore01 VMhost01 naa.60050768018086283000000000000187 False DetachedDatastore01 VMhost02 naa.60050768018086283000000000000188 False DetachedDatastore02 VMhost01 naa.60050768018086283000000000000187 False DetachedDatastore02 VMhost02 naa.60050768018086283000000000000188 False Detached.NOTESAuthor: Michael RyomDate: Febuary 5, 2013#>param([Parameter(Mandatory=$false)][string]$Command = 'get-help $MyInvocation.MyCommand.Definition -full',[Parameter(Mandatory=$false)][string]$vcenter,[Parameter(Mandatory=$false)][string[]]$datastores)$ErrorActionPreference = "SilentlyContinue"#ADD VMWARE SNAPINif(!(Get-PSSnapin | where {$_.Name -eq "VMware.VimAutomation.Core"})){add-pssnapin VMware.VimAutomation.Core}if(!(Get-PSSnapin | where {$_.Name -eq "VMware.VimAutomation.Core"})){Write-Host Could not load PowerCli snapin -foreground Redexit 1}#Import DatastoreFunctions.ps1 credit goes here http://blogs.vmware.com/vsphere/2012/01/automating-datastore-storage-device-detachment-in-vsphere-5.htmlImport-Module .\DatastoreFunctions.psm1 -DisableNameCheckingIf(Get-Module | where {$_.name -eq "DatastoreFunctions"}){Write-Host Module Loaded -foreground Blue}elseif(Get-ChildItem .\DatastoreFunctions.ps1){Write-Host Rename module DatastoreFunctions.ps1 to DatastoreFunctions.psm1 -foreground Redexit 1}else{Write-Host Module NOT loaded -foreground RedWrite-Host Go download the module at -foreground RedWrite-Host http://blogs.vmware.com/vsphere/2012/01/automating-datastore-storage-device-detachment-in-vsphere-5.html -foreground RedWrite-Host and save it to the same directory as this script -foreground RedWrite-Host Module should be named DatastoreFunctions.psm1 -foreground Redexit 1}#If not already logged in to vCenter, loginif($vcenter -ne ($global:DefaultVIServers | %{$_.Name}) -and $vcenter){Connect-VIServer $vcenter | Out-Null}if($vcenter -ne ($global:DefaultVIServers | %{$_.Name}) -and $vcenter){Write-Host Could not login to vCenter -foreground Redexit 1}ForEach ( $datastore in $datastores ) {function List {Write-host Listing $datastore -foreground yellowGet-Datastore $datastore | Get-DatastoreMountInfo | Sort Datastore, VMhost | FT -AutoSize}function Unmount {Write-host Unmounting $datastore -foreground yellowGet-Datastore $datastore | Unmount-Datastore}function Detach {Write-host Detaching $datastore -foreground yellowGet-Datastore $datastore | Detach-Datastore}function All {Write-host Unmounting $datastore -foreground yellowGet-Datastore $datastore | Unmount-DatastoreWrite-host Detaching $datastore -foreground yellowGet-Datastore $datastore | Detach-DatastoreWrite-host Listing $datastore -foreground yellowGet-Datastore $datastore | Get-DatastoreMountInfo | Sort Datastore, VMhost | FT -AutoSize}Invoke-Expression $Command}if($vcenter -eq ($global:DefaultVIServers | %{$_.Name})){Disconnect-VIServer -confirm:$false}

This article was updated on 17 Dec 2025