PowerCLI script to get Datastore UUIDs

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:

PowerShell

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-PSSnapinVMware.VimAutomation.Core

functionusage(){

Write-Host-foregroundcolorgreen"Use this script to get the actual Lun ID of a particular Datastore"

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.

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.

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?

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”

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! :)