Script to Detach Unmounted LUNs in SAN Connected Cluster

Script to Detach Unmounted LUNs in SAN Connected Cluster

<#

===========================================

Created By: Vishwanath Biradar

Created on: 02/Feb/2018

Version: 2.0(Final)

NOTE: The Script needs VCenter and Cluster name as inputs to detach Unmounted Luns and provide a CSV output that can be shared with storage team to reclaim
===========================================

#>
#Declaration of Variable for report
$report = @()
Function Login-VC {
$VCServer = Read-Host 'Enter VC Server name'
Write-Host "Connecting to $VCServer..." -Foregroundcolor "Yellow" -NoNewLine
$connection = Connect-VIServer -Server $VCServer -ErrorAction SilentlyContinue -WarningAction 0 | Out-Null
}


#Conection to Vcenter server
Login-VC
Write-Host " "
$cluster = Read-Host "Enter Cluster name ="
$time = (Get-Date).tostring("dd-MM-yyyy-hh-mm-ss")
$CSVname = "$cluster-$time"
$esxs= Get-cluster "$cluster" | get-vmhost -State Connected #Fetching all host name of cluster
foreach($esx in $esxs) #Loop for each ESXI host in cluster
{
$esxcli = Get-EsxCli -VMhost $esx -V2
Write-host "Processing host $esx"
foreach($ds in $esxcli.storage.filesystem.list.Invoke())
{
if(($ds.VolumeName -ne "") -and ($ds.Mounted -eq "false"))
{

$row = "" | select VMHost, MountPoint,Volume,MountState,Size,DeviceName
foreach($st in $esxcli.storage.vmfs.extent.list.invoke())
{
if($st.VolumeName -eq $ds.VolumeName)
{
$row.DeviceName = $st.DeviceName
$Arguments= $esxcli.storage.core.device.set.CreateArgs()
$arguments.device = $row.DeviceName
$arguments.state = "off"
$esxcli.storage.core.device.set.Invoke($arguments)
$row.VMHost = ($esx).Name
$row.MountPoint = $ds.MountPoint
$row.MountState = $ds.Mounted
$row.Volume = $ds.VolumeName
}
}
$report += $row
}
else
{
}
}
} $report | Export-CSV "D:\scripts\DetachDatastore\${CSVname}.csv"
Write-host "Go and find report at location D:\DetachDatastore\${CSVname}.csv" # Location of file
Write-Host "Disconnecting $VCServer..." -Foregroundcolor "Yellow" -NoNewLine #Disconnecting the Vcenter server
Disconnect-VIServer * -Confirm:$false

Labels (1)
Version history
Revision #:
1 of 1
Last update:
‎03-03-2021 02:38 PM
Updated by:
 
Contributors