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
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
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
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?
Hi,
Finally, it is working well. Thanks again lucD