Having an ESX cluster is nice, and adding shared LUNs can sometimes become click redundant (host, config, storage, rescan; repeat).

Since we’ve been migrating to a new storage array, we’ve been adding quite a few LUNs to different clusters on different vCenter servers, so I wanted an easy way to rescan everything.

So here’s my script:

#set the input options here as -vc and -cluster
param([string]$vc = "vc", [string]$cluster = "cluster")
 
#check to make sure we have both
if (($vc -eq "vc") -or ($cluster -eq "cluster"))
	{
	write-host `n "Rescan-Storage rescans all HBAs, then goes back and rescans VMFS for all hosts in a specific cluster" `n
	Write-host `n "Required parameters are vc and cluster" `n `n "Example: `"Rescan-Storage -vc drtapw0015001 -cluster drtcrl0011101`"" `n
	break
	}
 
#connect to VC
Connect-VIServer $vc
 
#get a list of physical servers in a specific cluster
$servers = get-vmhost -location $cluster |sort name
 
#rescan all HBAs first
foreach ($server in $servers)
	{
	write-host "Scan all HBAs on "$server
	get-VMHostStorage -VMHost $server -RescanAllHba
	}
 
#go back and rescan all VMFS
foreach ($server in $servers)
	{
	write-host "Rescan VMFS on "$server
	get-VMHostStorage -VMHost $server -RescanVmfs
	}
 
#done, lets disconnect
Disconnect-VIServer -confirm:$false

So you basically run rescan-storage.ps1 -vc vcenterservername -cluster clustername

-cluster isn’t only for clusters, though, you can target a host, a vDatacenter, and perhaps a folder (I only tested cluster and vDC).