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:
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!
By Rob October 25, 2010 - 2:34 pm
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
By Luke October 25, 2010 - 3:57 pm
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.
By Shawn October 25, 2010 - 5:34 pm
The error is a Powershell issue. You can suppress it in Powershell v2 by using the following format for Add-PSSnapin:
By Mike T November 12, 2010 - 3:51 pm
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.
By Luke November 12, 2010 - 7:54 pm
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.
By Luke November 19, 2010 - 5:01 pm
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?
By Sannkalp February 1, 2011 - 11:40 am
Is it possible to get system uuid of all esx host in vcenter via script?
By Luke February 2, 2011 - 2:09 pm
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
By Script to pull host UUID for VMware PowerCLI - ThepHuck February 2, 2011 - 2:45 pm
[...] A reader asked about pulling host UUIDs, so I wipped together this script [...]
By Marc March 4, 2011 - 11:25 am
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]
By Luke March 7, 2011 - 11:15 pm
Been really busy. I’ll see if I can take a look at this soon.
By Russ March 21, 2011 - 6:17 pm
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”
By Luke March 22, 2011 - 3:16 am
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
By Brent April 20, 2011 - 12:47 pm
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
By Luke April 20, 2011 - 1:46 pm
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:
By tsentekin January 5, 2012 - 5:51 pm
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:(
By NetApp SnapMirror & RDMs – Automated failover | ThepHuck March 5, 2012 - 11:07 am
[...] few months ago, a reader by the name of Tolga ŞENTEKİN came across This Post looking for something to do a little more. Tolga was looking to script out DR for some VMs he [...]
By How to install ESX Offline Bundles and how to deploy Dell Management plugin for vCenter « Clouded Place April 12, 2012 - 7:12 am
[...] http://thephuck.com/virtualization/powercli-script-to-get-datastore-uuids/ [...]