Count LUN Paths of each Datastore

Lately, we had multiple changes in our environments. A lot of them were related to zoning or storage provisioning. It annoyed me to check all Hosts whether als Paths are up or not. So I decided to write a Script, which runs through the vCenter and grabs all hosts, their mounted Datastores and the number of Paths to it.
Remember, before you run the script, you need to connect to vcenters.

## Connect to vCenter Server 
Connect-VIServer -Server <vcenter>

After establishing connection, you can run the following script:

## Get all Hosts from connected VIServers 
$esxi_list = Get-VMhost 
foreach ($esxi in $esxi_list) { 
	## Get List of all Datastores connected to this host 
	$datastore_list = Get-VMHost -Name $esxi | Get-Datastore | Where-Object {$_.ExtensionData.Info.GetType().Name -eq "VmfsDatastoreInfo"} 
	## Get ESXi-CLI $esxcli = Get-ESXCLI -VMHost $esxi -V2 
	foreach ($datastore in $datastore_list) { 
		## Get NAA for Datastore 
		$datastore_canonical = $datastore.ExtensionData.Info.Vmfs.Extent 
		## Get All Paths to current Datastore (across both vmhba) 
		$paths = $esxcli.storage.core.path.list.invoke() | Where-Object Device -eq $datastore_canonical.DiskName 
		## Create Object for Output 
		New-Object PSObject -Property @{ Paths = $paths.Count Host = $esxi.name Datastore = $datastore NAA = $datastore_canonical } 
	} 
}

Leave a Reply

Your email address will not be published.