miguelvelezwhit
Enthusiast
Enthusiast

Having trouble with foreach loops

After a great deal of research, failed prototypes, and an excess of caffeine and nicotine, I found the commands I needed for pulling the 'ServiceTag' / 'SerialNumber' from my ESXi hosts in vSphere 5.0 - 5.5.  As an example:

$esxcli = Get-Esxcli -VMHost <host name> -V2

$esxcli.hardware.platform.get.Invoke().SerialNumber

This formula makes up the script for getting that tag/serial number for that one host.  I must be brain dead today because I'm traveling in a few hours by car for about 3 hours - very mind numbing without music.  I can't seem to figure out the correct way to set it up to so that I get that information from each of the hosts in a vCenter.  For this exercise, I have 19 hosts, all UCS created, but they have their own tag as well.

Can anyone guide me towards the proper syntax for a foreach loop to perform my little script above?

Thanks in advance as always

4 Replies
jpsider
Expert
Expert

Are you talking something like this:

Once connected to your vCenter

$allHosts = get-vmHost

foreach ($vmhost in $allHosts( {

  write-host "Running ESXCLIv2 on host $vmhost"

  $esxcli = Get-Esxcli -VMHost $vmhost -V2

  $serialNum = $esxcli.hardware.platform.get.Invoke().SerialNumber

  #Do something with the serial Number here?

  write-host $serialNum

}

or am I missing something?

miguelvelezwhit
Enthusiast
Enthusiast

This is why I love VMware Communities.  If I don't have the answer, there are a whole lot of fellow VMware enthusiasts/evangelists like myself who will have the correct answer.

Thanks jpsider!!  I believe that's going to be the answer for my problem.  I'll code it in as soon as I get my first cup of coffee for the day.  Perhaps then, I'll be coherent and cognitive enough to test this and let you know the results.  Everything looks great, now I have to plug it in and include a statement which provides the name, vmhostname, manufacturer, model and of course serial number.

I'll let you know the results - if I do it wrong, I hope you can guide me where I misstepped. 

Thanks again.

DaHess_DNVGL
Contributor
Contributor

Why don't you just this simple one liner and be done with it?

Get-VMHostHardware

The default output will give you the Hostname, Manufacturer, Model, Serial Number, etc and you can tweak it to your needs.

You got to be at least on PowerCLI 6.0 R2 I for this to work.

Audit and Manage ESXi Hosts with PowerCLI - VMware PowerCLI Blog - VMware Blogs

I personally run the PowerCLI in the latest version 6.5 Release 1 and it works like a charme.

Just type in "Get-Help Get-VMHostHardware" to get some basic explanation and command examples, or just go directly to the online-help:

Get-VMHostHardware - PowerCLI VMware.VimAutomation.Core Help Reference

0 Kudos
LucD
Leadership
Leadership

You could also do this with a one-liner.

Get-VMHost | Get-Esxcli -V2 |

Select @{N='VMHost';E={$_.VMHost.Name}},

        @{N='Serial';E={$_.hardware.platform.get.Invoke().SerialNumber}}


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

0 Kudos