Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
PowerCLI Script that outputs a list of all LUN paths for all ESXi hosts
#requires -Version 2
#requires -PSSnapin VMware.VimAutomation.Core
function Get-LUNPathState {
No parameters needed. Just execute the script.
This script outputs the number of paths to each LUN.
Get-LUNPathState -VMhosts 'esx01'
Lists all LUN pats for ESXi host esx01.
$esxihosts = Get-VMHost
Get-LUNPathState -VMHosts $esxihosts
Lists all LUN paths for all ESXi hosts in $esxihosts
Author: Patrick Terlisten,, Twitter @PTerlisten
This script is provided "AS IS" with no warranty expressed or implied. Run at your own risk.
This script is based on:
This work is licensed under a Creative Commons Attribution NonCommercial ShareAlike 4.0
International License (
[Parameter(Mandatory = $true,Position = 0,HelpMessage = 'ESXi Host',ValueFromPipeline = $true)]
# Create empty hash table
$ReportLunPathState = @()
Write-Host -Object `n
Write-Host -Object "$($VMhosts.length) host(s) needs to be processed. Please wait..."
# Initialize counter variable
$i = 0
ForEach ($VMHost in $VMhosts) {
# Increment counter variable
# Get all datastores from the current host
$VMHostDatastores = Get-Datastore
# Get all disks devices from the current host
$VMHostScsiLuns = $VMHost | Get-ScsiLun -LunType disk
ForEach ($VMHostScsiLun in $VMHostScsiLuns) {
# Get LUN paths for each disk device
$VMHostScsiLunPaths = $VMHostScsiLun | Get-ScsiLunPath
# Count paths per disk device
$ReportLunPathState += ($VMHostScsiLunPaths | Measure-Object) | Select-Object `
-Property @{N = 'Hostname'; E = {$VMHost.Name}}, `
@{N = 'Datastore'; E = {$VMHostDatastores | Where-Object -FilterScript {($ | ForEach-Object -Process {$_.diskname}) -contains $VMHostScsiLun.CanonicalName}| Select-Object -ExpandProperty name}}, `
@{N = 'CanonicalName'; E = {$VMHostScsiLun.CanonicalName}}, `
@{N = '# of Paths'; E = {$_.Count}}, `
@{N = 'Path State'; E = {$VMHostScsiLunPaths.State}}
"Error was $_"
$line = $_.InvocationInfo.ScriptLineNumber
"Error was in Line $line"
{$ReportLunPathState | Format-Table -AutoSize}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment