Highlighted
Enthusiast
Enthusiast

I've a requirement to get the current Guest VM name with ESX host serial number where the guest VM currently residing on with the help of PowerCLI command - can some help here?

Jump to solution

Requirement:

example -

ABC is the guest VM name and it is residing on 123 ESX host. I need the guest VM name with the ESX Host serial number where it is residing currently by using PowerCLI command / script. Our environment is ESXi 6.0. Can some one help please. Thanks in advance.

1 Solution

Accepted Solutions
Highlighted
User Moderator
User Moderator

Not sure how you are doing your copy/paste, or which browser you are using, but there were some more missing blanks in the file.

Try the attached one.


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

View solution in original post

15 Replies
Highlighted
Hot Shot
Hot Shot

it's not fast but it does what you need

$vmsList = Get-VM

$array = @()

foreach($vm in $vmsList)

    {

    $row = "" | select Name,ESXiSN

    $row.Name = (Get-VM $vm).Name

    $row.ESXiSN = (Get-VMHostHardware -VMHost (get-vm $vm).VMHost).SerialNumber

    $array += $row

    }

$array | ft

Let me know if it works

Highlighted
User Moderator
User Moderator

Try like this.

The Get-VMHostHardware cmdlet relies on the CIM connection to the ESXi node being open, which is not always the case.

$vmName = 'MyVM'

Get-VM -Name $vmName |

Select Name,

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

    @{N='Serial';E={(Get-EsxCli -VMHost $_.VMHost).hardware.platform.get().SerialNumber}}


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

Highlighted
Enthusiast
Enthusiast

Yes, it is giving the output what I want. But, can I input the selected Guest VMs server names either in a text file so that, I can get only what I want instead the whole vCenter servers.

0 Kudos
Highlighted
Enthusiast
Enthusiast

It is throwing an error. Can you please re-check. And also I would like to input some of the ESX Host names to get the output instead entire vCenter server guest VMs information.

0 Kudos
Highlighted
Enthusiast
Enthusiast

Thanks much LucD. The below one worked for me -

Try like this

$vmNames = Get-Content -Path .\vmnames.txt

Get-VM -Name $vmNames |

Select Name,

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

    @{N='Serial';E={(Get-EsxCli -VMHost $_.VMHost).hardware.platform.get().SerialNumber}}

Is there a way to take the connected vCenter name and the current time stamp while creating a output file ?

0 Kudos
Highlighted
User Moderator
User Moderator

Try like this

$now = Get-Date

$vmNames = Get-Content -Path .\vmnames.txt

Get-VM -Name $vmNames |

Select Name,

    @{N='Time';E={$now}},

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

    @{N='Serial';E={(Get-EsxCli -VMHost $_.VMHost).hardware.platform.get().SerialNumber}},

    @{N='vCenter';E={$_.uid.split('@')[1].Split(':')[0]}}


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

0 Kudos
Highlighted
Enthusiast
Enthusiast

I'm sorry if I confused you on this.

The actual requirement is, the output file name should be created with the connected vCenter name (I connect to the vCenter by using Connet-VIServer command in PowerCLI CMD interface) with the running time stamp. Say example: if the vCenter name is TestVCenter and I run the this script on 01/18 11am - the output file format should be created like this -

TestVCenter-0118-1100.csv (something like this). Hope you got my requirement. Thanks in advance.

0 Kudos
Highlighted
User Moderator
User Moderator

You mean like this?

$vmNames = Get-Content -Path .\vmnames.txt

Get-VM -Name $vmNames |

Select Name,

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

    @{N='Serial';E={(Get-EsxCli -VMHost $_.VMHost).hardware.platform.get().SerialNumber}} |

Export-Csv -Path ".\$($global:DefaultVIServer.Name)-$(Get-Date -Format 'MMdd-HHmm').csv" -UseCulture -NoTypeInformation


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

Highlighted
Enthusiast
Enthusiast

It is throwing an error like below -

Get-Date : Cannot bind parameter 'Date'. Cannot convert value

"-FormatMMdd-HHmm" to type "System.DateTime". Error: "String was not

recognized as a valid DateTime."

At C:\Get-GuestVMNamesWithHostSNos\Get-VMNamesWithHostSNos-LucD-Script.ps1:6

char:63

+ ... $($global:DefaultVIServer.Name)-$(Get-Date -Format'MMdd-HHmm').csv"-U ...

+                                                ~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidArgument: (:) [Get-Date], ParameterBindin

   gException

    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerSh

   ell.Commands.GetDateCommand

Get-VM : A parameter cannot be found that matches parameter name

'Name$vmNames'.

At C:\Get-GuestVMNamesWithHostSNos\Get-VMNamesWithHostSNos-LucD-Script.ps1:2

char:8

+ Get-VM -Name$vmNames |

+        ~~~~~~~~~~~~~

    + CategoryInfo          : InvalidArgument: (:) [Get-VM], ParameterBindingE

   xception

    + FullyQualifiedErrorId : NamedParameterNotFound,VMware.VimAutomation.ViCo

   re.Cmdlets.Commands.GetVM

0 Kudos
Highlighted
User Moderator
User Moderator

Something obviously went wrong in your copy/paste.

This "-FormatMMdd-HHmm"  should say  "-Format 'MMdd-HHmm'"


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

Highlighted
Enthusiast
Enthusiast

date format issue fixed, but the in the output file, ESX host serial number not getting captured.

0 Kudos
Highlighted
User Moderator
User Moderator

Can you attach the script as you are running it?

There might be other copy/paste issues


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

0 Kudos
Highlighted
Enthusiast
Enthusiast

please find attached -

0 Kudos
Highlighted
User Moderator
User Moderator

Not sure how you are doing your copy/paste, or which browser you are using, but there were some more missing blanks in the file.

Try the attached one.


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

View solution in original post

Highlighted
Enthusiast
Enthusiast

thanks a lot, the attachment is worked perfectly. Have a good day!

0 Kudos