Skip to content
Switch branches/tags
Go to file
Latest commit 54b36d5 May 14, 2019 History
PowerShell script to unmount/detach datastores in vcenter.
1 contributor

Users who have contributed to this file

A script to unmount and detach datastores from VMWare vCenter.
This script unmounts datastores from vCenter if they are in maintenance mode, reside in the specified folder, and the name matches the specified regular expression.
The VMware PowerCLI module is required and a connection to vCenter (Connect-VIServer) must be established before running the script.
Unmount-Datastores.ps1 -VIServer vcenter.domain.local -NameRegex "Datastore\d\d" -Folder "Unmount"
This command will unmount and detach datastores in the vcenter.domain.local vCenter that reside in the datastore folder named Unmount with names that -match the regular expression "Datastore\d\d".
Author: Luke Arntz
Date: May 13, 2019
# Specifies a vCenter server name.
# Specifies a datastore folder name.
# Specifies a regular expression to -match datastore names.
$MaintenanceModeDatastores = Get-Datastore -Location (Get-Folder -Name $Folder) -Server $ViServer | where {$_.State -eq "Maintenance" -And $_.Name -match $NameRegex}
$UnMounted = @()
foreach ($MaintenanceModeDatastore in $MaintenanceModeDatastores)
$datastoreHostInfo = Get-VmHost -Server $viserver -Id ($MaintenanceModeDatastore.ExtensionData.Host.Key)
$CanonicalName = $MaintenanceModeDatastore.ExtensionData.Info.Vmfs.Extent[0].Diskname
Write-Host "Processing $($MaintenanceModeDatastore)..."
foreach ($vmHost in $datastoreHostInfo)
$unmount = [PSCustomObject]@{
Datastore = $MaintenanceModeDatastore.Name
DsUuid = $
LunUuid = (Get-ScsiLun -Server $viserver -VmHost $vmHost | where {$_.CanonicalName -eq $CanonicalName}).ExtensionData.Uuid
Host = $
CanonicalName = $CanonicalName
UnmountTime = $null  
$hostMountInfo = $MaintenanceModeDatastore.ExtensionData.Host | Where {$_.Key -eq $VMHost.Id}
if($hostMountInfo.MountInfo.Mounted -eq "True")
Write-Host "Unmounting $($unmount.Datastore) from host $($VMHost.Name)"
$hostStorageSystem = Get-View $VMHost.Extensiondata.ConfigManager.StorageSystem
} else {
write-host "$($unmount.Datastore) not mounted on $($VMhost.Name)"
$unmount.UnmountTime = Get-Date -Format "s"
$UnMounted += $unmount