Script

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.

 

 

<#

.SYNOPSIS
Remove_Datastores.ps1
.DESCRIPTION
This script makes it possible to Unmount, detach, list or all of the above for at list of datastore(s)
.PARAMETER Command
Unmount - Unmounts the datastore(s) on all host(s) where its mounted
Detach - Detaches the datastore(s) on all host(s) where its attached
List - List the datastore(s), VMhosts, Lun, mounted and state
All - First it unmounts the datastore(s), then it detaches the datastore(s) and lastly it lists the datastore(s)
.PARAMETER vcenter
Specify the vCenter to which you want to connect to, in order to get data from.
.PARAMETER datastores
Specify the datastore(s) - Use comma to seperate datastores.
If datastore contains spaces - The datastore needs to be inclosed in ""
.EXAMPLE
C:\PS>Remove_Datastores.ps1 list vcenter.local.domain datastore01,datastore02
.OUTPUTS
Datastore VMHost Lun Mounted State
--------- ------ --- ------- -----
Datastore01 VMhost01 naa.60050768018086283000000000000187 False Detached
Datastore01 VMhost02 naa.60050768018086283000000000000188 False Detached
Datastore02 VMhost01 naa.60050768018086283000000000000187 False Detached
Datastore02 VMhost02 naa.60050768018086283000000000000188 False Detached

.NOTES
Author: Michael Ryom
Date: 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 SNAPIN
if(!(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 Red
exit 1}

#Import DatastoreFunctions.ps1 credit goes here http://blogs.vmware.com/vsphere/2012/01/automating-datastore-storage-device-detachment-in-vsphere-5.html
Import-Module .\DatastoreFunctions.psm1 -DisableNameChecking
If(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 Red
exit 1
}else{
Write-Host Module NOT loaded -foreground Red
Write-Host Go download the module at -foreground Red
Write-Host http://blogs.vmware.com/vsphere/2012/01/automating-datastore-storage-device-detachment-in-vsphere-5.html -foreground Red
Write-Host and save it to the same directory as this script -foreground Red
Write-Host Module should be named DatastoreFunctions.psm1 -foreground Red
exit 1
}
#If not already logged in to vCenter, login
if($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 Red
exit 1}

ForEach ( $datastore in $datastores ) {

function List {
Write-host Listing $datastore -foreground yellow
Get-Datastore $datastore | Get-DatastoreMountInfo | Sort Datastore, VMhost | FT -AutoSize
}

function Unmount {
Write-host Unmounting $datastore -foreground yellow
Get-Datastore $datastore | Unmount-Datastore
}

function Detach {
Write-host Detaching $datastore -foreground yellow
Get-Datastore $datastore | Detach-Datastore
}

function All {
Write-host Unmounting $datastore -foreground yellow
Get-Datastore $datastore | Unmount-Datastore
Write-host Detaching $datastore -foreground yellow
Get-Datastore $datastore | Detach-Datastore
Write-host Listing $datastore -foreground yellow
Get-Datastore $datastore | Get-DatastoreMountInfo | Sort Datastore, VMhost | FT -AutoSize
}

Invoke-Expression $Command
}
if($vcenter -eq ($global:DefaultVIServers | %{$_.Name})){
Disconnect-VIServer -confirm:$false
}

Leave a Reply

Your email address will not be published. Required fields are marked *