VMware Cloud Community
sylvain82
Contributor
Contributor
Jump to solution

how to get a Lun number from StorageDeviceInfo property to match a DeviceName or DevicePath?

Hello,

I'm new in powershell script and I have a basic question. I try to get a LUN Number from a disk connected to an ESX server. I want this because I want to add a rdm disk to a VM but in vsphere we have to specify the device name which is unique. So to easily identify the rdm LUN I want to match the LUN Number with the device name in the property StorageDeviceInfo.

Is anyone know a easy way to do that?

Thank you

Sylvain

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

The following script produces a report with the information you require (I think).

$hostStor = Get-VMHost <ESX-hostname> | Get-VMHostStorage | Get-View

$hba = @{}
$hostSTor.StorageDeviceInfo.HostBusAdapter | %{
	$hba[http://$_.Key|http://$_.Key] = $_
}
$lun = @{}
$hostSTor.StorageDeviceInfo.ScsiLun | %{
	$lun[http://$_.Key|http://$_.Key] = $_
}

$report = @()
$hostStor.StorageDeviceInfo.ScsiTopology.Adapter | %{
	$hbaDevice = $hba[http://$_.Adapter|http://$_.Adapter].Device
	$_.Target | %{
		$targetNumber =  $_.Target
		$_.Lun | %{
			$row = "" | Select hbaDevice, targetNumber, lunNumber, lunDeviceName
			$row.hbaDevice = $hbaDevice
			$row.targetNumber = $targetNumber
			$row.lunNumber = $_.Lun
			$row.lunDeviceName = $lun[http://$_.ScsiLun|http://$_.ScsiLun].DeviceName
			$report += $row
		}
	}
}
$report

Since there are some square brackets in the script I attached the script as a file as well.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
3 Replies
LucD
Leadership
Leadership
Jump to solution

The following script produces a report with the information you require (I think).

$hostStor = Get-VMHost <ESX-hostname> | Get-VMHostStorage | Get-View

$hba = @{}
$hostSTor.StorageDeviceInfo.HostBusAdapter | %{
	$hba[http://$_.Key|http://$_.Key] = $_
}
$lun = @{}
$hostSTor.StorageDeviceInfo.ScsiLun | %{
	$lun[http://$_.Key|http://$_.Key] = $_
}

$report = @()
$hostStor.StorageDeviceInfo.ScsiTopology.Adapter | %{
	$hbaDevice = $hba[http://$_.Adapter|http://$_.Adapter].Device
	$_.Target | %{
		$targetNumber =  $_.Target
		$_.Lun | %{
			$row = "" | Select hbaDevice, targetNumber, lunNumber, lunDeviceName
			$row.hbaDevice = $hbaDevice
			$row.targetNumber = $targetNumber
			$row.lunNumber = $_.Lun
			$row.lunDeviceName = $lun[http://$_.ScsiLun|http://$_.ScsiLun].DeviceName
			$report += $row
		}
	}
}
$report

Since there are some square brackets in the script I attached the script as a file as well.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
sylvain82
Contributor
Contributor
Jump to solution

Thank you for this script. It is what I'm looking for. I have just to adapt it for my need but when I execute it I get the following error:

Index operation failed; the array index evaluated to null.

At :line:24 char:29

+ $row.lunDeviceName = $lun[http:// <<<< $_.ScsiLun|http:// <<<< $_.ScsiLun].DeviceName

the line concerned is :

$row.lunDeviceName = $lun[http://$_.ScsiLun|http://$_.ScsiLun].DeviceName

Any idea?

0 Kudos
sylvain82
Contributor
Contributor
Jump to solution

Hi,

Finally, it is working well. Thanks again lucD

0 Kudos