This post covers removing a multiple datastores from a single host at a time in vCenter with the use of PowerCLI. In the case where you have multiple hosts which have MANY MANY Datastores attached, its important to make sure you fully unmount and detach the DS from the host before removing storage connections.
Below is the code to make this happen
$datastores = 'DS1','DS2'
$startTime = Get-Date
$esxName = 'vmh.local'
foreach($datastoreName in $datastores){
$datastoreName
$esx = Get-VMHost -Name $esxName
$ds = Get-Datastore -Name $datastoreName
$canonicalName = $ds.ExtensionData.Info.Vmfs.Extent[0].DiskName
$storSys = Get-View $esx.Extensiondata.ConfigManager.StorageSystem
$device = $storsys.StorageDeviceInfo.ScsiLun | where {$_.CanonicalName -eq $canonicalName}
if($device.OperationalState[0] -eq 'ok'){
$StorSys.UnmountVmfsVolume($ds.ExtensionData.Info.Vmfs.Uuid)
}
$storSys.DetachScsiLun($device.Uuid)
}
$endTime = Get-Date
$executionTime = $endTime - $startTime
$execMins = [math]::Round($executionTime.TotalMinutes,2)
$execSecs = [math]::Round($executionTime.TotalSeconds,2)
Write-Output "Script took $execmins Minutes or $execSecs seconds to execute."
Comments
Post a Comment