Find LUN ID’s in VMware with Powershell
Determining the LUN ID for a specific LUN in your VMware Infrastructure used to be simple. It was listed as one of the properties of the datastore you selected in the VI Client. Nowadays, more often than not, I dont see the LUN ID in the vSphere Client. Instead, I see some sort of identifier like “EMC Fibre Channel Disk (sym.12673548127)”.
Even more unfortunate is the fact that all my scripts show the same identifier, where they used to show the LUN ID. So I decided to create a script that can translate the identifier (sometimes referred to as Canonical Name) back to a LUN ID.
By the way: in the vSphere Client, you can still find the LUN ID by opening the datastore’s properties window and clicking Manage Paths. Or you could write down the canonical name, switch to the devices view and look up the device there. That is essentially what my script does for you.
Here we go:
function Get-LunId { param($CanonicalName, $VMHost) #Get advanced host properties $VMHostView = Get-VMHost $VMHost | Get-View #Get two different collections with luns #The first one matches the canonical name with a key $HostLunsByCanonicalName = Get-VMHost $VMHost | Get-SCSILun #The second one matches the key with a lun id $HostLunsByKey = $VMHostView.Config.StorageDevice.ScsiTopology | ForEach {$_.Adapter} | ForEach {$_.Target} | ForEach {$_.Lun} #Use the first collection to translate the canonicalname into the key $Key = ($HostLunsByCanonicalName | Where {$_.CanonicalName -eq $CanonicalName}).Key #Use the second colleciton to find the luns with the key $MatchingLuns = $HostLunsByKey | Where {$_.ScsiLun -eq $Key} #Every path to the lun will be a match, #so let's deduplicate and see if there is really a single result $NumResults = ($MatchingLuns | Group-Object Lun | Measure-Object).Count If ($NumResults -gt 1) { #Multiple luns found $MatchingLuns | Group-Object Lun | ForEach {$_.Name} } ElseIf ($NumResults -eq 1) { #Single result found ($MatchingLuns | Select -First 1).Lun } Else { #No results found "No result" } } |
Download the script here: Get-LunId (rename to *.ps1)
Note that when you use this function inside a script where you loop through several LUNs/VMs/Datastores on the same host, you are able to speed up your script significantly by taking the Get-VMHost $VMHost | Get-View part outside of the function.
Enjoy!
Hugo
3 Responses to Find LUN ID’s in VMware with Powershell
Tags
Active Directory API bind order cleanup cluster CPU Custom Fields datastores description device management directory tree errors Event Log file name filter Fun function HA IT known issues License Server LUN multipath NIC objects Oneliner portgroups PowerCLI PowerShell profile recursive Registry Scripts security session share snapshots SQL Stat VI Toolkit VMware vSphere WMI WSUS ZenArchives
- July 2012
- July 2011
- February 2011
- January 2011
- December 2010
- May 2010
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008





Great Function, I am new to Powershell and scripting !!
How do I capture the output to a csv ?
Thanks Rob
does not work…
does not work for me too…