{"id":2012,"date":"2024-06-18T11:25:00","date_gmt":"2024-06-18T15:25:00","guid":{"rendered":"https:\/\/enterpriseadmins.org\/blog\/?p=2012"},"modified":"2024-12-27T14:03:37","modified_gmt":"2024-12-27T19:03:37","slug":"converting-between-guest-os-serial-number-at-vm-uuid","status":"publish","type":"post","link":"https:\/\/enterpriseadmins.org\/blog\/scripting\/converting-between-guest-os-serial-number-at-vm-uuid\/","title":{"rendered":"Converting between guest OS serial number and VM UUID"},"content":{"rendered":"\n<p>A customer recently asked me if it was possible to find a virtual machine BIOS serial number from a virtual machine object or vmx file.  While looking into this, I found an old post that provided a very useful hint: <a href=\"https:\/\/peppercrew.nl\/2011\/04\/get-virtual-machine-bios-serial-number\/\">https:\/\/peppercrew.nl\/2011\/04\/get-virtual-machine-bios-serial-number\/<\/a>.  This post contains a PowerShell function to convert a VM UUID to the BIOS serial number.  Looking at the code, and reviewing some test VMs, I realized that this is only a string replacement exercise.  I&#8217;ve seen these serial numbers and UUIDs for years and never made the connection that they were so closely related.<\/p>\n\n\n\n<p>The image below has one example value.  The top line is the virtual machine serial number obtained from inside the guest OS (using <code>wmic bios get serialnumber<\/code> on Windows or <code>dmidecode -s system-serial-number<\/code> on Linux).  The bottom line is the virtual machine UUID (from PowerCLI we can find this with: <code>$vm.ExtensionData.Config.Uuid<\/code>).  The color coding has been added to make the pattern easier to see.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/enterpriseadmins.org\/blog\/wp-content\/uploads\/2024\/06\/image.png\"><img loading=\"lazy\" decoding=\"async\" width=\"804\" height=\"104\" src=\"https:\/\/enterpriseadmins.org\/blog\/wp-content\/uploads\/2024\/06\/image.png\" alt=\"\" class=\"wp-image-2014\" srcset=\"https:\/\/enterpriseadmins.org\/blog\/wp-content\/uploads\/2024\/06\/image.png 804w, https:\/\/enterpriseadmins.org\/blog\/wp-content\/uploads\/2024\/06\/image-300x39.png 300w, https:\/\/enterpriseadmins.org\/blog\/wp-content\/uploads\/2024\/06\/image-768x99.png 768w\" sizes=\"auto, (max-width: 804px) 100vw, 804px\" \/><\/a><figcaption class=\"wp-element-caption\">Color coded comparison of VM serial number and UUID<\/figcaption><\/figure>\n\n\n\n<p>This actually helped explain something I&#8217;ve seen before with virtual machines created by storage array-based clones having duplicate UUIDs.  The top value (minus the &#8216;VMware-&#8216; prefix) is stored in the vmx file as <code>uuid.bios<\/code>.  When a direct clone of the VM file is registered into inventory, the VMs UUID (and by extension, BIOS serial number) would be a duplicate of the original\/source VM.<\/p>\n\n\n\n<p>We can see in the following command\/output that two VMs have different names \/ IDs, but have duplicate UUIDs.  I&#8217;ve confirmed with <code>dmidecode<\/code> that these two guests also have the same system-serial-number.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Get-VM h207-vm-* | Select-Object Name, ID, @{N='UUID';E={$_.extensiondata.config.uuid}}\n\nName       Id                        UUID\n----       --                        ----\nh207-vm-01 VirtualMachine-vm-3147125 42023081-331b-58e1-2730-ca560789e551\nh207-vm-02 VirtualMachine-vm-3147127 42023081-331b-58e1-2730-ca560789e551<\/code><\/pre>\n\n\n\n<p>If we want to change our VM UUID, to ensure all our VMs have unique serial numbers, we can do that with PowerCLI as well.  For illustration purposes, I&#8217;m going to update the UUID value for the second VM to end with the number 2.  However, we could also have PowerShell generate a completely new UUID with <code>[guid]::NewGuid()<\/code>.  <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$vm = Get-VM h207-vm-02\n$spec = New-Object VMware.Vim.VirtualMachineConfigSpec\n$spec.uuid = '42023081-331b-58e1-2730-ca560789e552'\n$vm.extensiondata.ReconfigVM_Task($spec)<\/code><\/pre>\n\n\n\n<p>The above code will update our second VM to have a UUID which ends in 552 (the string we provided).  In the below code block we&#8217;ll get the same two VMs as above and note that our UUIDs have changed.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Get-VM h207-vm-* | Select-Object Name, ID, @{N='UUID';E={$_.extensiondata.config.uuid}}\n\nName       Id                        UUID\n----       --                        ----\nh207-vm-01 VirtualMachine-vm-3147125 42023081-331b-58e1-2730-ca560789e551\nh207-vm-02 VirtualMachine-vm-3147127 42023081-331b-58e1-2730-ca560789e552<\/code><\/pre>\n\n\n\n<p>Knowing the BIOS serial number and UUID relationship can be understood with string manipulation, I created a quick function to reverse the original example we found.  In this function we can provide a VM system-serial-number and have it select the pieces of the string required to build our UUID.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function Get-VMUuidFromSerial {\n  param($vmSerial)\n  if ($vmSerial.length -ne 54) { write-warning \"The provided serial number $vmSerial does not appear to be the correct length.\" }\n\n  $myResult = $vmSerial -Replace 'VMware-', '' -Replace ' ', ''\n  $myResult.substring(0, 8) + '-' + $myResult.substring(8,4) + '-' + $myResult.SubString(12,4) + '-' + $myResult.Substring(17,4) + '-' + $myResult.Substring(21, $myResult.length - 21)\n}<\/code><\/pre>\n\n\n\n<p>The original code we found expected a parameter of type <code>[VMware.VimAutomation.ViCore.Impl.V1.Inventory.VirtualMachineImpl]<\/code>, but our VM objects were being passed in as <code>[VMware.VimAutomation.ViCore.Impl.V1.VM.UniversalVirtualMachineImpl]<\/code>, so I updated the type definition in the function. To be able to test this using a list of UUIDs from a CSV file, I ended up removing that type definition and checked the length of the string, similar to the above function. I also adjusted the function name to match the format we used above.  I&#8217;m including it below for reference\/safe keeping.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function Get-VMSerialFromUUID {\n  param($vmUUID)\n  if ($vmUUID.length -ne 36) { write-warning \"The provided UUID $vmUUID does not appear to be the correct length.\" }\n\n  $myResult = 'VMware-'\n  $serialnumtmp = $vmUUID.Replace('-','')\n  for ($i = 0; $i -lt $serialnumtmp.length; $i += 2) { $myResult += $serialnumtmp.substring($i, 2); if ($myResult.Length -eq 30) {$myResult += '-' } else { $myResult += ' ' } }\n  $myResult\n}<\/code><\/pre>\n\n\n\n<p>Hopefully this post will help if you ever need to compare VM UUIDs with BIOS serial numbers and\/or resolve duplicate serial number issues.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A customer recently asked me if it was possible to find a virtual machine BIOS serial number from a virtual machine object or vmx file. While looking into this, I found an old post that provided a very useful hint: &hellip; <a href=\"https:\/\/enterpriseadmins.org\/blog\/scripting\/converting-between-guest-os-serial-number-at-vm-uuid\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[3,4],"tags":[],"class_list":["post-2012","post","type-post","status-publish","format-standard","hentry","category-scripting","category-virtualization"],"_links":{"self":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts\/2012","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/comments?post=2012"}],"version-history":[{"count":4,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts\/2012\/revisions"}],"predecessor-version":[{"id":2159,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts\/2012\/revisions\/2159"}],"wp:attachment":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/media?parent=2012"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/categories?post=2012"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/tags?post=2012"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}