Get LUN id of Raw Disk Mappings with PowerCLI

I had a need to get the LUN id of all of the Raw Disk Mappings for a particular virtual machine running on VMware vSphere. I could see this information within the vSphere Client by editing the setting of the virtual machine, selecting each of the Raw Disk Mapping Hard Disks one at a time and clicking on the Manage Paths button in the bottom right hand corner. The LUN id is then displayed for each path to the LUN in the LUN column, the LUN id is also the last part of the Runtime Name – displayed in the first column, e.g.


This is a time consuming process, especially if you need to perform it for a large number of virtual machines. Also I do not like editing the settings of a virtual machines just to look at the setting as I may accidently change something and forget to click on cancel instead of OK. So I looked at getting this information with PowerCLI.

You can get a list of hard disks the virtual machine has with the following one liner

Get-VM | Get-HardDisk

You can then limit this information to just the Raw Disk Mappings as follows:

Get-VM | Get-HardDisk | Where {$_.DiskType –eq “RawPhysical”}

However the properties returned do not include the LUN id. From the SCSI Canonical Name you can find out the Runtime Name by using the Get-SCSILun command against one of the hosts, of course this should be the host the virtual machine is running on which is returned in the VMHost property from Get-VM. So if I run Get-SCSILun for the SCSI Canonical Name of each of the Raw Disk Mappings I can get the Runtime Name. Then if I use the Substring method on the Runtime Name to strip off the number of L at the end of the Runtime Name, by using the LastIndexof method, I can get the LUNid. The following commands achieve this:

$Disks = Get-VM | Get-HardDisk | Where {$_.DiskType -eq “RawPhysical”}
Foreach ($Disk in $Disks) {
$Lun = Get-SCSILun $Disk.SCSICanonicalName -VMHost (Get-VM ).VMHost
$Lun.RuntimeName.Substring($Lun.RuntimeName.LastIndexof(“L”)+1)
}

Where is the name of the virtual machine you want to get the LUN ids for.

I have put all of this into a script and only run the commands if the virtual machine passed to the script exists, i.e.

Param($VM)

if (Get-VM $VM) {

$Disks
= Get-VM $VM | Get-HardDisk | Where {$_.DiskType -eq
“RawPhysical”}

Foreach ($Disk
in
$Disks) {

$Lun
= Get-SCSILun $Disk.SCSICanonicalName -VMHost (Get-VM $VM).VMHost

$Lun.RuntimeName.Substring($Lun.RuntimeName.LastIndexof(“L”)+1)

}

}

I saved this script in C:\VIscripts as Get-VMLUNid.ps1 and run it as follows: –

C:\VIscripts\Get-VMLUNid.ps1

e.g.

C:\VIscripts\Get-VMLUNid.ps1 sqlsvr01


This entry was posted in PowerCLI, Storage, VMware. Bookmark the permalink.

3 Responses to Get LUN id of Raw Disk Mappings with PowerCLI

  1. Tolga says:

    this really helps thanks,
    i want to ask how can we list the LunID’s for all the Luns mapped to vcenter,
    what i want to accomplish is finding “eq: lun with lunID 3” and add to a spesific vm.

    Thanks again.

  2. I presume you mean list all the LUN IDs for the LUNs mapped to the ESX(i) hosts.
    You can do this with get-nalunbyigroup by specifying the igroup and the LUN id, e.g. if the igroup you have used for your ESXi hosts is called VMwareServers and you want to know which LUN is ID 3 then you run get-nalunbyigroup VMwareServers 3. This will give you the details of the LUN on the NetApp.

    If you are trying to add a RDM to a VM then you do not need to know the LUN details from the NetApp as when you go to add the LUN via the vSphere Client you are presented with a list of LUNs not already being used, one of the columns is the LUN id. I’ve not tried configuring a RDM via PowerShell so not sure what you need and don’t have time to look a it at the moment, but it is possible that you need the scsidisk key of the LUN, you can get this with

    get-scsilun -vmhost | ? {$_.Runtimename -like “vmhba?:C?:T?:L”} | select key

    e.g. to get the scsi id of LUN 3 mapped to host esxi01.mydomain

    get-scsilun -vmhost esxi01.mydomain | ? {$_.Runtimename -like “vmhba?:C?:T?:L3”} | select key

  3. lance0507 says:

    Thank you!

Leave a Reply to CalvinScoltock Cancel reply

Your email address will not be published. Required fields are marked *