VMware Cloud Community
azhskr
Enthusiast
Enthusiast
Jump to solution

Get VM Serial Number from List of VM's?

Alright.  I have been smashing my head against this for hours and finally have given up and am asking for help.  I have this function below that I found at http://www.peppercrew.nl/index.php/2011/04/get-virtual-machine-bios-serial-number/.

It works fantastic for one individual VM, however I have a list of 250 VM's that I need this information for, as our asset management software requires a unique serial number for each VM.

I have tried modifying the script to import from a .txt file and run the information through in a foreach loop, and just am not having any success.  What am I doing wrong?

function Get-VMSerial{

$VMList = Get-Content C:\VMList.txt    foreach ($VM in $VMList){
        $VirtualMachine = Get-VM $vm.name

    param([VMware.VimAutomation.ViCore.Impl.V1.Inventory.VirtualMachineImpl]$VirtualMachine)
 
    $s = ($VirtualMachine.ExtensionData.Config.Uuid).Replace("-", "")
    $Uuid = "VMware-"    for ($i = 0; $i -lt $s.Length; $i += 2)
    {
        $Uuid += ("{0:x2}" -f [byte]("0x" + $s.Substring($i, 2)))
        if ($Uuid.Length -eq 30) { $Uuid += "-" } else { $Uuid += " " }
    }
 
    Write-Output $Uuid.TrimEnd()}



}
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

How does your CSV look ?

Do you have a column header in there ? Something like

Name

VM1

VM2

In that case you can change the last line in

Get-VM (Import-CSV C:\VMList.csv | Select -ExpandProperty Name) | Select Name,BIOSNumber 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
7 Replies
LucD
Leadership
Leadership
Jump to solution

One solution is to integrate that function in a New-VIProperty definition.

Then the BIOSNumber becomes a property on a VirtualMachine.

Something like this

New-VIProperty -Name BIOSNumber -ObjectType VirtualMachine -Value {
  param($vm)

  $s = ($vm.ExtensionData.Config.Uuid).Replace("-", "")
  $Uuid = "VMware-"
 
for ($i = 0; $i -lt $s.Length; $i += 2)   {     $Uuid += ("{0:x2}" -f [byte]("0x" + $s.Substring($i, 2)))     if ($Uuid.Length -eq 30) { $Uuid += "-" } else { $Uuid += " " }   }   $Uuid.TrimEnd() } -Force | Out-Null
Get-VM
(Import-CSV C:\VMList.csv) | Select Name,BIOSNumber


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

azhskr
Enthusiast
Enthusiast
Jump to solution

This is probably a dumb question, but is the correct way to test your code just to copy and paste it into PowerCLI?  When I do that I get the error message below:

At line:4 char:22
+   $Uuid = "VMware-"  for ($i = 0; $i -lt $s.Length; $i += 2)
+                      ~~~
Unexpected token 'for' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnexpectedToken

If I copy your code into a new function in my profile, when PowerCLI loads it gives me the following error:

At C:\Users\username\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1:27 char:21
+   $Uuid = "VMware-" for ($i = 0; $i -lt $s.Length; $i += 2)
+                     ~~~
Unexpected token 'for' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : UnexpectedToken

There were a couple of extra spaces I deleted out of there to test that, but didn't seem to make any difference.  Ideas?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

It seems a CR-LF was dropped during the copy/paste.

It's corrected, please try again.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
azhskr
Enthusiast
Enthusiast
Jump to solution

When I run this now, I receive the following error message for each entry in my CSV file.  The CSV file has one column that says Name, and then the list of all the servers underneath of it.  I replaced my actual servername with ServerName in the error code below.

Get-VM : 2/13/2013 1:17:33 PM    Get-VM        VM with name '@{Name=ServerName}' was not found using the specified filter(s).
At line:12 char:1
+ Get-VM (Import-CSV C:\VMList.csv) | Select Name,BIOSNumber
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (:) [Get-VM], VimException
    + FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVM

EDIT:

I think something weird is going on.  If I remove the name field from the CSV, I get the following error.  ServerName1 is the first entry listed in the .CSV.  ServerNameX is any other server in the list.  So it's going through all 250 of them and throwing an error for every single server, with ServerName1.

Get-VM : 2/13/2013 1:27:09 PM    Get-VM        VM with name '@{ServerName1=ServerNameX}' was not found using the specified filter(s).
At line:1 char:1
+ Get-VM (Import-CSV C:\VMList.csv) | Select Name,BIOSNumber
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (:) [Get-VM], VimException
    + FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVM

All the code before the line below completes without error if I paste it into PowerCli.

Get-VM (Import-CSV C:\VMList.csv) | Select Name,BIOSNumber

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

How does your CSV look ?

Do you have a column header in there ? Something like

Name

VM1

VM2

In that case you can change the last line in

Get-VM (Import-CSV C:\VMList.csv | Select -ExpandProperty Name) | Select Name,BIOSNumber 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
azhskr
Enthusiast
Enthusiast
Jump to solution

EDIT:  That did the trick.  For some reason it looks like I was losing my connection to the vCenter servers which may have contributed to the error.  This is exactly what I needed.  Thank you for your help.

Correct, it looks like:

Name

VM1

VM2

VM3

VM4

..VM250

Your code fixed the ServerName1 = ServerNameX issue, however now it just returns the errors below:

Get-VM : 2/13/2013 1:46:26 PM    Get-VM        VM with name 'ServerNameX' was not found using the specified filter(s).
At line:1 char:1
+ Get-VM (Import-CSV C:\VMList.csv | Select -ExpandProperty Name) | Select Name,BI ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (:) [Get-VM], VimException
    + FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVM

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

That seems to say that there is no VM with the name ServerNameX.

Does

Get-VM -Name ServerNameX

return anything or do you get the same error ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos