Quick Post: Removing Datastore Tags and Mounts with PowerCLI
Over the past couple of weeks i’ve been helping our Ops Team decommission an old storage array. Part of the process is to remove the datastore mounts and paths to ensure a clean ESXi Host config as well as remove any vCenter Tags that are used for vCloud Director Storage Policies. Looking through my post archive I came across this entry from 2013 that (while relating to ESXi 4.1) shows you that there can be bad consequences if you pull a LUN from a host in the incorrect manner. Also if you are referencing datastores through storage policies and (http://anthonyspiteri.net/quick-tip-disapearing-datastore-cluster-in-vcloud-after-adding-datastore-to-vcenter-with-storage-profiles/) an incorrectly removed datastore will throw errors for the Virtual DC and Provider vDC from where the datastores used to be referenced. With that, below is the process I refined with the help of an excellent set of PowerCLI commandlets provided by the Module created by Alan Renouf. Step 1 - Remove Any vCenter Tags:
#
#Remove Tags
Get-Datastore | where { $_.Name -Match 'DATASTORE_NAME|GROUP' } | Get-TagAssignment | Remove-TagAssignment
#Example Output
PowerCLI C:\Files\scripts> Get-Datastore STRG01-TP0-L003 | Get-TagAssignment | Remove-TagAssig
nment -Verbose
VERBOSE: 7/07/2015 10:26:46 AM Remove-TagAssignment Started execution
Confirm
Are you sure you want to perform this action?
Performing operation "Remove tag assignment of tag 'MyCloud-Tag'" on Target "STRG01-TP0-L003".
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): A
VERBOSE: 7/07/2015 10:26:50 AM Remove-TagAssignment Finished execution
VERBOSE: 7/07/2015 10:26:50 AM Remove-TagAssignment Started execution
VERBOSE: 7/07/2015 10:26:50 AM Remove-TagAssignment Finished execution
VERBOSE: 7/07/2015 10:26:50 AM Remove-TagAssignment Started execution
VERBOSE: 7/07/2015 10:26:50 AM Remove-TagAssignment Finished execution
VERBOSE: 7/07/2015 10:26:50 AM Remove-TagAssignment Started execution
VERBOSE: 7/07/2015 10:26:50 AM Remove-TagAssignment Finished execution
After this has been done you can go into vCloud Director and Refresh the Storage Policies which will remove the datastores from the Providers. Step 2 - Import Datastore Function Module:
#
#Import Datastore Function Module
Import-Module .\DatastoreFunctions.ps1
#Example Output
PowerCLI C:\Files\scripts> Import-Module .\DatastoreFunctions.ps1 -Verbose
VERBOSE: Loading module from path 'C:\Files\scripts\DatastoreFunctions.ps1'.
Security warning
Run only scripts that you trust. While scripts from the internet can be useful, this script can potentially harm your computer.
Do you want to run C:\Files\scripts\DatastoreFunctions.ps1?
[D] Do not run [R] Run once [S] Suspend [?] Help (default is "D"): R
VERBOSE: Dot-sourcing the script file 'C:\Files\scripts\DatastoreFunctions.ps1'.
Step 3 - Connect to vCenter, Dismount and Detach Datastore
#
#Connect to vCenter
Connect-VIServer VC_NAME
#Get Datastore Info:
Get-Datastore | where { $_.Name -Match 'DATASTORE_NAME|GROUP' } | Get-DatastoreMountInfo
#Unmount Datastores:
Get-Datastore | where { $_.Name -Match 'DATASTORE_NAME|GROUP' } | Unmount-Datastore
#Detach Datastore:
Get-Datastore | where { $_.Name -Match 'DATASTORE_NAME|GROUP' } | Detach-Datastore
#Example Output
PowerCLI C:\Files\scripts> Get-Datastore STRG01-TP0-L003 | Unmount-Datastor
Unmounting VMFS Datastore STRG01-TP0-L003 from host node-136...
Unmounting VMFS Datastore STRG01-TP0-L003 from host node-127...
Unmounting VMFS Datastore STRG01-TP0-L003 from host node-130...
Unmounting VMFS Datastore STRG01-TP0-L003 from host node-126...
PowerCLI C:\Files\scripts> Get-Datastore STRG01-TP0-L003 | Detach-Datastore
Detaching LUN naa.6006016091b038006df0748e2f33e411 from host node-136...
Detaching LUN naa.6006016091b038006df0748e2f33e411 from host node-127...
Detaching LUN naa.6006016091b038006df0748e2f33e411 from host node-130...
Detaching LUN naa.6006016091b038006df0748e2f33e411 from host node-126...
What the above commands do is check to see what Hosts are connected to the datastore being removed and what paths exist. You then run the Unmount command to unmount from the host and the Detach command removes all the paths from the host. Step 4 - Refresh Storage on Hosts
#
#Refresh Storage on Hosts
Get-VMhost | Get-VMHostStorage -Refresh
#Example Output
PowerCLI C:\Files\scripts> Get-VMHost | Get-VMHostStorage -Refresh
SoftwareIScsiEnabled
--------------------
True
True
True
True
The last step is to refresh the storage to remove all reference of the datastore from the host. I did encounter a problem on a couple of hosts during the unmount process that returned the error as shown below:
PowerCLI C:\Files\scripts> Get-Datastore STRG01-TP0-L022 | Unmount-Datastore
Unmounting VMFS Datastore PER02-STRG01-TP0-L022 from host node-112.zettagrid.com...
Exception calling "UnmountVmfsVolume" with "1" argument(s): "The resource 'Datastore Name: STRG01-TP0-L022 VMFS uuid:
541bcd24-75a0a31a-38b5-848f691183cf' is in use. Cannot unmount volume 'Datastore Name: STRG01-TP0-L022 VMFS uuid:
541bcd24-75a0a31a-38b5-848f691183cf' because file system is busy. Correct the problem and retry the operation."
At C:Files\scripts\DatastoreFunctions.ps1:101 char:6
+ $StorageSys.UnmountVmfsVolume($DS.ExtensionData.Info.vmfs.uuid);
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : VimException
This error is actually caused by a VSAN module that actively stores traces needed to debug any VSAN related issues on VMFS datastores…not really cool when VSAN isn’t being used, but the fix is a simple one as specified in this KB. References: http://blogs.vmware.com/vsphere/2012/01/automating-datastore-storage-device-detachment-in-vsphere-5.html https://communities.vmware.com/docs/DOC-18008