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.
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
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
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
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.
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.
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 ?
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
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.
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
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
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
date format issue fixed, but the in the output file, ESX host serial number not getting captured.
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
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
thanks a lot, the attachment is worked perfectly. Have a good day!