A reader on a previous post asked about pulling host UUIDs, so I wipped together this script.
Usage is like this:
Get-VMHostUUID.ps1 -vmhosts ("host1","host2","host3")
or
Get-VMHostUUID.ps1 -vc vcenterserver -container cluster1/folder/dc/etc
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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
param([string]$vc = "vc", [string]$container = "container", [string[]]$vmhosts = $null) #add the snapin, just in case Add-PSSnapin VMware.VimAutomation.Core #Show Usage function usage() { Write-host -foregroundcolor green `n`t"This script is used to pull UUIDs for all hosts provided." Write-host -foregroundcolor green `n`t"You can either specify -vmhosts as an array:" write-host -foregroundcolor yellow `n`t`t"Get-VMHostUUID.ps1 -vmhosts (`"host1`",`"host2`",`"host3`")" write-host -foregroundcolor green `n`t"or specify -vc and -container, where container is a host name, cluster, folder, datacenter, etc:" write-host -foregroundcolor yellow `n`t`t"Get-VMHostUUID.ps1 -vc vcenterserver -container cluster1" `n write-host -foregroundcolor green `t"if -container is not used, it pulls ALL hosts from the given vCenter" `n write-host -foregroundcolor green `t"You can use either -vmhosts by itself, or -vc, not a combination of them, and -container is optional." `n } function Get_UUID() { foreach ($vmhost in $vmhosts) { if ($esx -eq 1) #do this only if connecting directly to ESX hosts { connect-viserver $vmhost -credential $vmhost_creds > $NULL 2>&1 } $vmhostUUID = (Get-VMHost $vmhost | Get-View).hardware.systeminfo.uuid Write-Host `n Write-Host -foregroundcolor green "Server: " $vmhost write-host -foregroundcolor green "UUID: " $vmhostUUID if ($esx -eq 1) #disconnect from the current ESX host before going to the next one { disconnect-viserver -confirm:$false } } if ($vcenter -eq 1) #disconnect from vcenter { disconnect-viserver -confirm:$false } } #check to make sure we have all the args we need if (($vmhosts -eq $null) -and ($vc -eq "vc")) #if vmhosts and vc is not supplied { usage break } elseif (($vmhosts -ne $null) -and ($vc -ne "vc")) #if both vmhosts and vc was supplied { usage break } elseif (($vmhosts -ne $null) -and ($container -ne "container")) #if both vmhosts and container was supplied { usage break } elseif (($vmhosts -ne $null) -and (($vc -eq "vc") -or ($container -eq "container"))) #if only vmhosts is used, set our esx variable to 1 and get credentials { $esx = 1 $vmhost_creds = $host.ui.PromptForCredential("ESX/ESXi Credentials Required", "Please enter credentials to log into the ESX/ESXi host.", "", "") Get_UUID } elseif (($vmhosts -eq $null) -and (($vc -ne "vc") -and ($container -eq "container"))) #if vc and container are used, set our vcenter variable to 1, get credentials, and populate vmhosts { $vcenter = 1 $vc_creds = $host.ui.PromptForCredential("vCenter Credentials Required", "Please enter credentials to log into vCenter.", "", "") connect-viserver $vc -credential $vc_creds > $NULL 2>&1 $vmhosts = get-vmhost | sort name Get_UUID } elseif (($vmhosts -eq $null) -and (($vc -ne "vc") -and ($container -ne "container"))) #if vc and container are used, set our vcenter variable to 1, get credentials, and populate vmhosts { $vcenter = 1 $vc_creds = $host.ui.PromptForCredential("vCenter Credentials Required", "Please enter credentials to log into vCenter.", "", "") connect-viserver $vc -credential $vc_creds > $NULL 2>&1 $vmhosts = get-vmhost -location $container | sort name Get_UUID } #garbage collection $vmhost_creds = $null $vc_creds = $null $vmhosts = $null $vc = $null $container = $null $esx = $null $vcenter = $null |
Output is something like this:
Server: server1
UUID: 12345678-90ab-cdef-1234-567890abcdef
Server: server2
UUID: 12345678-90ab-cdef-1234-567890abcdef
Server: server3
UUID: 12345678-90ab-cdef-1234-567890abcdef
****Updated on Feb 3, 2011 @ 1:15pm central time
I updated the script. The -container parameter is now optional. If left off, it pulls all hosts in the vCenter you’re connected to.
Thanks Luke for sending me script.
When i am running above script it’s giving me below error message. I have vCenter 4.0 U2 and want script to collect ESX System UUID from all ESX servers whihc are registered in vCenter. i am running this script on vSphere PowerCLI.. Please suggest where i am doing wrong.
[vSphere PowerCLI] E:\> .\get-uuid.ps1 -vmhosts (“abzssvm012″,”abzssvm013”)
You cannot call a method on a null-valued expression.
At E:\get-uuid.ps1:16 char:32
+ $InputTypeName = $_.GetType( <<< .\get-uuid.ps1 -vc hacssvc001
You cannot call a method on a null-valued expression.
At E:\get-uuid.ps1:16 char:32
+ $InputTypeName = $_.GetType( <<<
Error MEssage: –
[vSphere PowerCLI] E:\> .\get-uuid.ps1 -vmhosts (“abzssvm012″,”abzssvm013”)
You cannot call a method on a null-valued expression.
At E:\get-uuid.ps1:16 char:32
+ $InputTypeName = $_.GetType( <<<< ).Name
Please pass this script either a VMHost or VM object on the pipeline.
I didn’t test the -vmhosts switch, only -vc & -container. I’ll test it real quick and see what I did wrong.
I’ve tried both options and it works for me, I’m also on vCenter 4.0u2.
What happens if you manually connect to vCenter, and then run:
(Get-VMHost abzssvm012 | Get-View).hardware.systeminfo.uuid
Thanks Luke for your quick response!
As instructed by you i pasted the attached script and ran the command as mentioned and i also connected to by vCenter named “hacssvc001” but now it’s giving me below error message. Please help.
[vSphere PowerCLI] E:\> .\Get-VMHostUUID.ps1 (Get-VMHost abzssvm012 | Get-View).
hardware.systeminfo.uuid
Missing closing ‘}’ in statement block.
At E:\Get-VMHostUUID.ps1:11 char:102
+ write-host -foregroundcolor yellow
n
tt"Get-VMHostUUID.ps1 -vmhosts (
”host1
",
“host2",
“host3`”) <<<< "Luke…freakin awesome! I needed this to get my powerpath VE licenced. Took me less than 5 mins with powergui to get the info. Big thanks for making it easy!
There’s a much easier way than going through all that.
Get-VMhost | Get-View | % { $_.hardware.systeminfo.uuid }
That’s it. If you want to get fancy and have a need for speed (if you have many VMhosts):
Get-View -ViewType HostSystem -Propert Name, hardware.systeminfo -Filter @{“Name”=”VMHostNameHere” } | % { $_.hardware.systeminfo.uuid }
If you want to get even fancier and return the host name with the UUID, here’s a little trick using SELECT:
Get-View -ViewType HostSystem -Propert Name, hardware.systeminfo -Filter @{“Name”=”VMHostNameHere” } | Select Name, @{N=”UUID” ; E={$_.hardware.systeminfo.uuid } }
Notice the “N=” and “E=” clauses? N means NAME and E means expression.
Good call! The guts of my function are essentially the same thing:
$vmhostUUID = (Get-VMHost $vmhost | Get-View).hardware.systeminfo.uuid
It’s just designed as a script to pull from every host, or hosts in a specific folder or cluster, and with some logic. I could have used the select function for the name, but I don’t think I had learned about it when I originally wrote this script. It was more designed for “connect to that vcenter and get me all the UUIDs for hosts in this cluster”.Hello,
I am able to retrieve all of the UUIDs for every guest VM using the following:
get-vm $vm | ForEach-Object {(get-view $_.id).config.uuid} | Out-File d:\ps\uuid.txt
How do I display the name and UUID of each guest?
Regards,
Brian
There are a few ways to do it, I can point you in the right direction, just been really busy.
You could do this by using select.
First, create your array variable that will hold all the data, we’ll call it $list
Then, we’ll need get a list of all your vms & loop through them to populate the rows or items: