Switch Fabric Got Your VMs Down?

So, interesting question posed at work.  With impending fibre channel switch replacements coming down the line, it was asked whether we could gracefully disable the paths going to a particular switch prior to the change.

After a bit of Google’ing, I came across Matt Bradford’s site (VMSpot) where he’d already done most of the work for us.

Reference: Disable ESXi fibre ports using PowerCLI

On initial run, it failed out as it seems the LUN path identifiers are different now on vSphere 6, so we’ve modified it accordingly.

As stated on VMSpot, and I *strongly* reiterate, test in lab first and use with extreme caution.  This is a script to remove storage paths….typically that’s a bad thing.  🙂

#=========================
# Disable Fibre Paths
# Matt Bradford (www.vmspot.com)
# 1/24/2015
# 
# 
# 
# Version 2
# 5/2/2017
# www.cloudharbinger.com
# 
# Changes: 
# * Updated selection menu to include HBA device name, hostname, and current state. 
# * SCSI Path selection now uses "Name" attribute, rather than LunPath attribute, which seems to be deprecated
# 
#=========================

#Collect ESXi host name from command line
[cmdletbinding()]
Param([string]$VMhost)

#Load the Vmware Powershell Snapin (For users executing script from powershell)
Add-PSSnapin VMware.VimAutomation.Core -ErrorAction 'SilentlyContinue'

# Change "vcenter.fqdn" to targeted vCenter system
$vcenter = "vcenter.fqdn"

#Connect to vCenter
Write-Host ""
Write-Host "Connecting to vCenter..." -NoNewline
Connect-VIServer $vcenter  | Out-Null
Write-Host " Connected"
Write-Host ""

#Get WWPN's from host
$WWNs = Get-VMHost $VMhost | Get-VMHostHba -Type FibreChannel | Select Device,VMHost,@{N="WWN";E={"{0:X}"-f$_.PortWorldWideName}},@{N="Status";E={$_.ExtensionData.Status}}


#Create a selection menu from WWPN's
Write-Host "WWPN's Available:"

$i = 0

foreach ($WWN in $WWNs)
{
 $WWN_name = $WWN.Device
 $WWN_WWN = $WWN.WWN
 $WWN_host = $WWN.VMHost
 $WWN_status = $WWN.Status
 Write-Host "$i $WWN_name $WWN_WWN $WWN_host $WWN_status" -ForegroundColor 'Green'
 $i ++
}

Write-Host ""
$Selection = Read-Host "Select a path to disable"

$WWN = $WWNs[$Selection].Device

#Validate that the user wants to disable ports
Write-Host ""
Write-Host "DISABLE $WWN " -ForegroundColor 'Red' -NoNewline
$validate = Read-Host "ARE YOU SURE? Y/N"

if ($validate -eq "Y")
{
 #Get each disk device 
 $scsilun = Get-ScsiLun -VMHost $VMhost -LunType disk
 
 #Get paths that use the selected WWPN
 $scsipaths = $scsilun | Get-ScsiLunPath | Where-Object {$_.Name -Like "*$WWN*"}


 # Disable / Enable each path using the selected WWPN
 foreach ($scsipath in $scsipaths)
 {

# Disables the paths. Comment out to re-enable
 Set-ScsiLunPath -ScsiLunPath $Scsipath -Active $false

# To re-enable the paths, uncomment the following
 # Set-ScsiLunPath -ScsiLunPath $Scsipath -Active $true

}


}
elseif ($validate -eq "N")
{
 Write-Host ""
 Write-Host "User Abort Process. Exiting" -ForegroundColor 'Red'
 Write-Host ""
 Exit
}
else
{
 Write-Host ""
 Write-Host "Invalid Entry. Exiting" -ForegroundColor 'Red'
 Write-Host ""
 Exit
}