Awhile back a guy at the San Antonio VMUG asked the technical group how you could get the actual LUN UUID for a particular Datastore. I informed him that it was available via the PowerCLI and to contact me via the VMUG forums. He never did. My storage guy at work loves this script, though, so I thought I’d share it with everybody.
Here’s the script:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
param([string]$vc = "vc" , [string]$vmhost = "vmhost" , [string]$ds = "ds") Add-PSSnapin VMware.VimAutomation.Core function usage() { Write-Host -foregroundcolor green "Use this script to get the actual Lun ID of a particular Datastore" Write-Host "`n" Write-Host -foregroundcolor green "Usage:" Write-Host -foregroundcolor yellow "`tGet-LunID -vc <virtualcenter hostname> -vmhost <ESX host name> -ds <Datastore name>" Write-Host "`n" Write-Host -foregroundcolor green "The VC hostname must be a hostname for which you have stored credentials (see New-VICredentialStoreItem)!" Write-Host -foregroundcolor green "The ESX host must be the full name of a host that can access the Datastore." Write-Host "`n" } function glid([string]$vmhost,[string]$dsname) { $ds = Get-View (Get-View (Get-VMHost -Name $vmhost).ID).ConfigManager.StorageSystem foreach ($vol in $ds.FileSystemVolumeInfo.MountInfo) { if ($vol.Volume.Name -eq $dsname) { Write-host "DS Name:`t" $vol.Volume.Name Write-host "VMFS UUID:`t" $vol.Volume.Uuid foreach ($volid in $vol.Volume.Extent) { foreach ($lun in $ds.StorageDeviceInfo.MultipathInfo.Lun){ if ($lun.Id -eq $volid.DiskName) { break } } } Write-Host "LUN Name:`t" $lun.ID $mid = $lun.ID foreach ($id in $ds.StorageDeviceInfo.ScsiLun) { if ($id.CanonicalName -eq $mid) { $uuid = $id.Uuid Write-host "LUN UUID:`t" $uuid } } } } } if (($vc -eq "vc") -or ($vmhost -eq "vmhost") -or ($ds -eq "ds")) { usage } else { $cd = Get-VICredentialStoreItem -host $vc Connect-VIServer -server $cd.host -user $cd.user -password $cd.password Write-Host "`n" glid $vmhost $ds disconnect-viserver -confirm:$False } |
Expected output should be something like this:
DS Name: DRTCRL0011101_VM_002
VMFS UUID: 481f5e2a-142ab69b-9ce3-001422168f72
LUN Name: vmhba1:2:1
LUN UUID: 0200010000600601605f1119008b9bac46a017dd11524149442035
This information may be useful when dealing on the storage side of things so you don’t pull the wrong LUN!
Has anyone seen this error when running this script?
Add-PSSnapin : Cannot add Windows PowerShell snap-in VMware.VimAutomation.Core
because it is already added. Verify the name of the snap-in and try again.
At C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\Get-LunID.ps
1:2 char:13
+ Add-PSSnapin <<<< VMware.VimAutomation.Core
+ CategoryInfo : InvalidArgument: (VMware.VimAutomation.Core:Stri
ng) [Add-PSSnapin], PSArgumentException
+ FullyQualifiedErrorId : AddPSSnapInRead,Microsoft.PowerShell.Commands.Ad
dPSSnapinCommand
This comes from the second line of the script: Add-PSSnapin VMware.VimAutomation.Core
Whenever you run the script from the VMware PowerCLI, the plugin is already added, so you get the error.
If you run the script from a regular powershell window, it will load the needed VMware plugin and you won’t get the error.
Without that line, you won’t get the error in the VMware PowerCLI, but the script won’t run from a regular powershell window.
You can ignore it or remove the line, it’s really up to you. All of my scripts give this error because I have the VMware plugin loaded through my profile on a standard powershell window, so when the script runs it throws the error because it can’t load a plugin that’s already loaded.
The error is a Powershell issue. You can suppress it in Powershell v2 by using the following format for Add-PSSnapin:
This is nice, it works for 1 host & 1 datastore UUID. Someone should figure out how to loop this so it will enumerate the hosts in a vCenter, then enumerate each datastore on each host, and then list out the UUID of each of those datastores. Then compare the UUID for like datastore names to assure they are the same. I need this for an environment that is controlled by vCenter, has no clusters, but we want to move to clusters. First must assure UUID for each datastore name is identical.
Adding host enumeration isn’t that hard, I can add that functionality. If Shawn doesn’t have time to edit, I can do it, since this is his script.
Just rereading your comment and enumerating each datastore on each host, then comparing is kind of pointless. If they’re in a cluster and can see the shared storage, then the datastore name will show up the same. In the simplest form, a header is written to the datastore, like a GUID, so any host in vCenter that can see it, will register it as the same name. If you change the name of the datastore on one host, it changes on all hosts.
You’re basically wanting to pull UUIDs for every datastore on every host, correct?
Is it possible to get system uuid of all esx host in vcenter via script?
Yes, I’ll write a quick script for this, but this is basically the line to pull the host UUID:
(Get-VMHost hostname | Get-View).hardware.systeminfo.uuid
Hi, i got on your site from google.
I have a problem with a script I’m working on.
I’d like to get the lun number or maybe something like vmhba0:C0:T0:L0 like in the Expected output ou write on the top. Presently, the LUN column is empty… Can you help me?
Thanks.
here it is.
Connect-VIServer $args[0]
$LD = @()
Get-VMHost | % {
$myHost = $_
$ds = Get-View (Get-View (Get-VMHost -Name $myHost).ID).ConfigManager.StorageSystem
foreach ($vol in $ds.FileSystemVolumeInfo.MountInfo) {
$objPath = “” |Select-Object Host, DataStore, VMFS, LUN, LUNUUID
$mid = $lun.ID
foreach ($id in $ds.StorageDeviceInfo.ScsiLun) {
$uuid = $id.Uuid
$objPath.Host = $myHost
$objPath.DataStore = $vol.Volume.Name
$objPath.VMFS = $vol.Volume.Uuid
$objPath.LUN = $lun.ID
$objPath.LUNUUID = $uuid
$LD += $objPath
}
}
}
$LD | Export-Csv -NoTypeInformation -Path $args[1]
Been really busy. I’ll see if I can take a look at this soon.
I can’t figure out what I’m doing wrong! Every time I try and run the script it returns the usage.
Use this script to get the actual Lun ID of a particular Datastore
Usage:
Get-LunID -vc -vmhost -ds
The VC hostname must be a hostname for which you have stored credentials (see New-VICredentialStoreItem)!
The ESX host must be the full name of a host that can access the Datastore.
I’ve run the command New-VICredentialStoreItem to put an entry in for VC. Here’s how I’m running it .\LUN_UUID.ps1 -vc “virtual_centre_hostname” -vihost “FQDN_to_ESXServer” -ds “The_Name_of_the_DataStore_case_sensitive”
The only thing I see right off is you’re using vihost instead of vmhost. I just tested that on my end and it causes what you’re seeing. I tried with and without the quotes and it works either way:
and
I have a related question (sort of). Anybody have a tip on setting a datastore to the one with the most free space? This is for the purposes of doing some mass VM deployment across multiple datastores.
$esxhost = “hostname”
$datastore = get-datastore -vmhost $esxhost ???
Hoping there’s an easy way to do this so I’m not manually setting a datastore for every VM. I’m relatively new to powershell but attempting to learn at a rapid pace! :)
Thanks in advance for the assistance
Brent
You’ll kick yourself when you realize how easy it actually is, you just need to add | sort FreeSpaceMB -descending, so your code looks like this:
To pull the entry at the top of the list (the one with the most free space), you just need to pull the first item of the array, like this:
hi,
i’ve found you via google, i’m trying to get only the Lun ID’s of the luns mapped to the esx host
which is eq: LUN Name: vmhba3:C:T0:L1
and then i want to strip out the last number (“1” at the end) which is the actual lun id given by the storage device
is this so hard to do? i’ve been trying for 2 days that took me nowhere near:(
Was looking for API mapping between datastore and corresponding ScsiLUN for a while. This webpage answered my queries. Thank you.
I really like this script, however in vCenter 6.0.1, I do not get the LUN UUID. There is no match here when there should be. Have you tested this in vCenter 6?
I have not tested it on anything relatively new, it was written a long time ago. I’ll see if I can update it to work with 6.x, though, as I have time!
Thanks!
Luke