VMware Cloud Community
ganapa2000
Hot Shot
Hot Shot
Jump to solution

How to get LUN ID ?

Hi,

How do I get the LUN ID, When I execute the below script, it runs successfully but LUN ID shows blank.

Please help.

Get-VM crpt01 | Get-HardDisk |

Select @{N='VMName';E={$_.Parent.Name}},

@{N='HDName';E={$_.Name}},

@{N="Datastore";E={Get-Datastore -VM $_}},

@{N='NAA';E={$_.ScsiCanonicalName}},

@{N="Lunid";E={$_Parent.RuntimeName.Split(':')[3].Trim('L')}} |

Export-Csv C:\test001.csv -NoTypeInformation

Reply
0 Kudos
1 Solution

Accepted Solutions
Craig_Baltzer
Expert
Expert
Jump to solution

Have a look at vNugglets: Get VM Disks and RDMs via PowerCLI and see if it looks like what you're trying to do. It uses the View method which is typically much faster than the cmdlets from the first example.

VMware KB: Identifying virtual disks pointing to Raw Device Mappings (RDMs) gives an example of how to get just the RDM disks by using the -DiskType "RawPhysical","RawVirtual" parameter on Get-HardDisk

View solution in original post

Reply
0 Kudos
6 Replies
Craig_Baltzer
Expert
Expert
Jump to solution

Are you trying to get the LUN ID of the hard disk in the VM, or the LUN ID of the datastore? The code

@{N="Lunid";E={$_Parent.RuntimeName.Split(':')[3].Trim('L')}}

is looking at $_ and what's in the pipeline is a VM hard disk object, who's parent is a VM. A VM doesn't have a property RuntimeName.

So if you want the LUN ID of the hard disk in the VM use

@{N="Lunid";E={$_.ExtensionData.UnitNumber}}


Keep in mind that if you have multiple SCSI controllers in the VM then you may have more than one hard disk with the same LUN ID

The LUN of the datastore is a bit more complicated as you need to backtrack from the hard disk -> datastore -> SCSI LUN.

@{N="Lunid";E={($_ | Get-Datastore | Get-SCSILun | Select-Object -First 1).RuntimeName.Split(':')[3].Trim('L')}}

I've seen cases where RuntimeName isn't populated for SAN connected disk. In that case you may have to get it from one of the LUN paths instead of the LUN itself. The assumption in the code is that best practices have been followed and the storage is presented as the same LUN # on all paths, so picking the first active one is fine

@{N="Lunid";E={($_ | Get-Datastore | Get-SCSILun | Select-Object -First 1 | Get-SCSILunPath | Where-Object { $_.State -eq "Active"} | Select -First 1).LunPath.Split(':')[3].Trim('L')}}

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Hi,

Appreciate your help, I tried with below lines in two different instances

@{N="Lunid";E={($_ | Get-Datastore | Get-SCSILun | Select-Object -First 1).RuntimeName.Split(':')[3].Trim('L')}}


@{N="Lunid";E={($_ | Get-Datastore | Get-SCSILun | Select-Object -First 1 | Get-SCSILunPath | Where-Object { $_.State -eq "Active"} | Select -First 1).LunPath.Split(':')[3].Trim('L')}}


When I ran the script, it is taking more time, I didn't get the output even after waiting for 15 mins then I had to kill the session. There was no error, it is just running without any output.




Reply
0 Kudos
Craig_Baltzer
Expert
Expert
Jump to solution

Get-SCSILun and Get-SCSILunPath are very slow, so just to verify things are working I'd start with a VM that has a single hard disk. For an environment with say 100 VMs it could run for hours easily...

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Craig,

Thanks for your reply. I am running the script only for one VM, still taking very long.

Can you help me, If the script takes long, how to get the LUN ID for RDM disks which are presented to VM only, ignoring VMDK (Thin or Thick disks)

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Hi Powercli Guru,

Can someone help in modifying the script, I want only LUN ID for RDM LUNs only

Reply
0 Kudos
Craig_Baltzer
Expert
Expert
Jump to solution

Have a look at vNugglets: Get VM Disks and RDMs via PowerCLI and see if it looks like what you're trying to do. It uses the View method which is typically much faster than the cmdlets from the first example.

VMware KB: Identifying virtual disks pointing to Raw Device Mappings (RDMs) gives an example of how to get just the RDM disks by using the -DiskType "RawPhysical","RawVirtual" parameter on Get-HardDisk

Reply
0 Kudos