New Script – Get VM or ESX Host UUIDs (get-uuid.ps1)

by bunchc on April 29, 2009

UUIDs are wonderful! Really. They’re just not all that easy to get to, at least not when you need more than a few of them at a time. That is where this script comes in:

# get-uuid.ps1
#
# Takes either a VMHost or VM object from the pipeline, returns the corresponding UUID.
Begin {
    $VMHost_UUID = @{
        Name = "VMHost_UUID"
        Expression = { $_.Summary.Hardware.Uuid }
    }
    $VM_UUID = @{
        Name = "VM_UUID"
        Expression = { $_.Config.Uuid }
    }
}

Process {
    $InputTypeName = $_.GetType().Name
    if ( $InputTypeName -eq "VMHostImpl" ) {
        $_ | Get-View | Select-Object $VMHost_UUID
    } elseif ( $InputTypeName -eq "VirtualMachineImpl" ) {
        $_ | get-view | Select-Object $VM_UUID
    } else {
        Write-Host "`nPlease pass this script either a VMHost or VM object on the pipeline.`n"
    }
}

Excusing the line wrap and odd orange color, you can see in the “Begin” block, that we define the two properties, based on their location in the API. In the process block we do some error checking, and then grab the relevant UUIDs.

The output looks similar to:

[VI Toolkit] C:\> get-vm | select -first 1 | .\scripts\get-uuid.ps1

VM_UUID
——-
50205819-19c6-965d-8d70-dbad7c43e598

[VI Toolkit] C:\> get-vmhost | select -first 1 | .\scripts\get-uuid.ps1

VMHost_UUID
———–
44454c4c-4a00-104d-8042-cac04f544831

The script can be downloaded here. Please note, that it was discovered while researching this post that the VMHost UUIDs stored by vCenter 2.5 U3 may or may not actually be unique. I have an active SR around this, and will make a follow up post once we get it sorted.

Previous post:

Next post: