Quantcast
Channel: admin – PEETERSONLINE.NL
Viewing all articles
Browse latest Browse all 20

Find LUN ID’s in VMware with Powershell

$
0
0

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


Viewing all articles
Browse latest Browse all 20

Trending Articles