Friday, February 15, 2013

PowerShell - Show VMWARE VM UUID

How to show the VMWARE UUID of a VM using PowerShell

I am working on a project where one of the functions is to get the UUID of a VM from the guest vm via PowerShell. You can get the "Windows" UUID with:

  • (Get-WmiObject Win32_ComputerSystemProduct).UUID
The problem is that a lot of the values are reversed. Below are examples of a Windows UUID and a VMWARE UUID.
  • Windows UUID
    • F8420542-10A9-9961-5225-C6F8D91A468D
  • VMWARE UUID
    • 420542f8-a910-6199-5225-c6f8d91a468d
So i needed to figure out a way to move the variables around. To do this, I used ".substring" with delimiters to extract out each value, and then piece it back together. I am not a PowerShell expert in any means, so if you think there is a better way to accomplish any of what I am doing, there probably is. 
  • NOTE: There is another way of doing this with "vSphere PowerCLI" and Get-VM commands. I did not want to go that route, because I did not want to download or install anything to accomplish my task. I wanted to do this from a PowerShell CLI.

The Script:


##
## Begin Script vmware_show_uuid.ps1
##


######################################################
# "Find VMWARE UUID"
# By: Keith Smith - smith.itpro@gmail.com
# Date: 2/15/2013
######################################################

# Set Variables
$uuid = (Get-WmiObject Win32_ComputerSystemProduct).UUID

# Section 1
$uuid11 = $uuid.Substring(0,2)
$uuid12 = $uuid.Substring(2,2)
$uuid13 = $uuid.Substring(4,2)
$uuid14 = $uuid.Substring(6,2)

# Section 2
$uuid21 = $uuid.Substring(9,2)
$uuid22 = $uuid.Substring(11,2)

# Section 3 
$uuid31 = $uuid.Substring(14,2)
$uuid32 = $uuid.Substring(16,2)

# Section 4
$uuid41 = $uuid.Substring(19,4)

# Section 5
$uuid51 = $uuid.Substring(24,12)

# Piece the strings together
[string]$uuida = "$uuid14$uuid13$uuid12$uuid11"
[string]$uuidb = "$uuid22$uuid21"
[string]$uuidc = "$uuid32$uuid31"
[string]$uuidd = "$uuid41"
[string]$uuide = "$uuid51"
[string]$uuidfixed = "$uuida-$uuidb-$uuidc-$uuidd-$uuide"

# Clear everything on the screen and pop up the VMWARE UUID
clear
$msgbox = new-object -comobject wscript.shell
$x = $msgbox.popup("$uuidfixed",0,"VMWARE UUID",1)

##
## End Script
##




2 comments:

  1. Since the UUID is really a GUID you can swap the bytes of the first DWORD at once, same with the two WORD's after that:

    $Uuid = (Get-WmiObject Win32_ComputerSystemProduct).UUID
    $VMwareUuid = ("{0:x}-{1:x}-{2:x}-{3}" -f [System.Net.IPAddress]::HostToNetworkOrder([Int32]("0x" + $Uuid.Substring(0,8))), [System.Net.IPAddress]::HostToNetworkOrder([Int16]("0x" + $Uuid.Substring(9,4))), [System.Net.IPAddress]::HostToNetworkOrder([Int16]("0x" + $Uuid.Substring(14,4))), $Uuid.Substring(19)).ToLower()

    ReplyDelete
  2. or...

    $Uuid = ([Guid](Get-WmiObject Win32_ComputerSystemProduct).UUID)
    $ba = $Uuid.ToByteArray()
    [System.Array]::Reverse($ba, 0, 4)
    [System.Array]::Reverse($ba, 4, 2)
    [System.Array]::Reverse($ba, 6, 2)
    $VMwareUuid = [Guid]$ba

    ReplyDelete