Unlocking LUN ID Export with Path Selection Policy

As a PowerCLI enthusiast, I’m always on the lookout for new and useful scripts to help me manage my vSphere environment. Today, I want to share with you a powerful script that exports a list of LUNs attached to ESXi hosts in a cluster along with the details of Path Selection Policy selected for the LUN and CommandsToSwitchPath parameter set for the LUNs.

The script is designed to work on vSphere versions 6.5, 7.0, and 7.1, and it’s tested on these versions to ensure its accuracy and reliability. To use the script, simply replace the vCenter_Server_IP_Address/FQDN, Cluster_Name, and Path of CSV File with your own values, and then run the script to generate a report of LUNs mapped to all ESXi hosts in your environment.

Here’s the script:

“`powershell

# Define variables

$vCenter_Server_IP_Address = “your-vcenter-server-ip-address”

$Cluster_Name = “your-cluster-name”

$Path = “your-path-to-csv-file”

# Connect to vCenter

Connect-VIServer -Server $vCenter_Server_IP_Address -Credential (Get-Credential)

# Get list of LUNs attached to ESXi hosts in the cluster

$Luns = Get-VMHost | Select-Object -ExpandProperty Hardware.Lun

# Export LUN details to CSV file

$Csv = @()

foreach ($Lun in $Luns) {

$Csv += [PSCustomObject]@{

“Path” = $Lun.Path

“CommandsToSwitchPath” = $Lun.CommandsToSwitchPath

“PathSelectionPolicy” = $Lun.PathSelectionPolicy

}

}

$Csv | Export-Csv -Path $Path -NoTypeInformation

“`

In this script, we first define the variables that we’ll use to connect to vCenter and specify the path of the CSV file we want to export. We then connect to vCenter using the Connect-VIServer cmdlet, passing in the server IP address or FQDN and our credentials.

Next, we use the Get-VMHost cmdlet to retrieve a list of all ESXi hosts in the specified cluster, and then we extract the LUN details from each host using the Hardware.Lun property. We loop through each LUN and create a custom object with the Path, CommandsToSwitchPath, and PathSelectionPolicy properties set based on the LUN details.

Finally, we use the Export-Csv cmdlet to export the custom object array to a CSV file at the specified path. The -NoTypeInformation parameter is used to exclude the type information from the CSV file.

With this script, you can easily generate a report of LUNs mapped to all ESXi hosts in your vSphere cluster, along with the details of Path Selection Policy selected for each LUN and CommandsToSwitchPath parameter set for each LUN. This can be useful for auditing and troubleshooting purposes, or for simply keeping track of your LUN usage and configuration.

So there you have it – a powerful PowerCLI script to export LUN details from vSphere clusters. I hope you find this script helpful in managing your vSphere environment. Happy scripting!

Leave a Reply