PowerCLI: Get Storage Paths of ESXi servers from vCenter

I needed to make an automated script to get all the Storage paths for all ESXi servers. To be noted that the script will retrieve only the ESXis with VMFS datastores and collect the information.

Script

# Retrieve vCenter, ESXi, cluster, and datastore information
$vcu = ""

Connect-VIServer -Server $vcu

$vCenterInfo = $vcu
$esxiHosts = Get-VMHost | Where-Object {
    $datasto = $_ | Get-Datastore
    $datasto | ForEach-Object { $_.ExtensionData.Summary.Type -ne "NFS" }
}
$clusters = Get-Cluster
$datastores = Get-Datastore

$results = @()

foreach ($esxiHost in $esxiHosts) {

    $hbaInfo = Get-VMHostHba -VMHost $esxiHost
    
    foreach ($hba in $hbaInfo) {

        $paths = Get-ScsiLun -Hba $hba

        foreach ($path in $paths) {
           
            $VMHostScsiLunPaths = $path | Get-ScsiLunPath

            
            foreach ($scsiPath in $VMHostScsiLunPaths) {
                $datastore = $datastores | ? {$_.ExtensionData.Info.Vmfs.Extent[0].DiskName -eq $path.canonicalName}

                $property = [ordered]@{
                    "vCenter" = $vCenterInfo
                    "ESXi" = $esxiHost.Name
                    "Cluster" = $esxiHost.Parent.Name
                    "Datastore" = $datastore.Name
                    "DatastoreType" = $datastore.ExtensionData.Summary.Type
                    "CanonicalName" = $path.CanonicalName
                    "PathPolicy" = $path.Multipathpolicy
                    "LUN" = (($path.RunTimeName -Split "L")[1] -as [Int])
                    "HBA" = $hba.Device
                    "Source" = "{0}" -f ((("{0:x}" -f $hba.PortWorldWideName) -split '([a-f0-9]{2})' | where {$_}) -Join ":")
                    "HBAname" = $path.RunTimeName
                    "Status" = $scsiPath.State
                    "Target" = $scsiPath.SanID
                    "Path" = $scsiPath.Name
                    "PathState" = $scsiPath.ExtensionData.PathState
                }

                # Add the object to the results array
                $results += New-Object -TypeName psobject -Property $property
            }
        }
    }
}
$results | Export-Csv -Path "C:\temp\output.csv" -NoTypeInformation

Disconnect-VIServer -Server $vcu -Confirm:$false

Loading