VMware Cloud Community
tdubb123
Expert
Expert
Jump to solution

get-hardware info script

getting an error from this. just wanted a second pair of eyes to check what I am doing wrong

$results= @()

foreach($dc in get-datacenter){

    foreach($cluster in get-cluster -location $dc) {

        foreach ($vmhost in $cluster) {       

$esxcli = get-esxcli -VMHost $vmhost

$info = "" | select esxHost, cluster, Datacenter, ESXVErsion, VendorNAme, Productname, SErialNumber

$info.esxHost = $vmhost.name

$info.cluster = $cluster.name

$info.DAtacenter = $dc.name

$info.VendorName = $esxcli.hardware.platform.get().VendorName

$info.Productname = $esxcli.hardware.platform.get().Productname

$info.Serialnumber = $esxcli.hardware.platform.get().Serialnumber

$results += $info

}}}

$results | Out-GridView

Reply
0 Kudos
1 Solution

Accepted Solutions
pezh0re
Enthusiast
Enthusiast
Jump to solution

I think your inner foreach needs tweaking:

foreach($dc in get-datacenter){

    foreach($cluster in get-cluster -location $dc) {

        foreach ($vmhost in $($cluster | Get-VMHost)) { 

^^ that will return an array of VMhost objects and let you to proceed. (I'm away from my environment, so that may not be the exact syntax)

View solution in original post

Reply
0 Kudos
5 Replies
LucD
Leadership
Leadership
Jump to solution

Which error?


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

Reply
0 Kudos
tdubb123
Expert
Expert
Jump to solution

error is

Get-EsxCli : Cannot bind parameter 'VMHost'. Cannot convert the "xxxxxxx" value of type "VMware.VimAutomation.ViCore.Impl.V1.Inventory.ClusterImpl" to type

"VMware.VimAutomation.ViCore.Types.V1.Inventory.VMHost".

At line:7 char:30

+ $esxcli = get-esxcli -VMHost $vmhost

+                              ~~~~~~~

    + CategoryInfo          : InvalidArgument: (:) [Get-EsxCli], ParameterBindingException

    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,VMware.VimAutomation.ViCore.Cmdlets.Commands.EsxCli.GetEsxCli

Reply
0 Kudos
pezh0re
Enthusiast
Enthusiast
Jump to solution

I think your inner foreach needs tweaking:

foreach($dc in get-datacenter){

    foreach($cluster in get-cluster -location $dc) {

        foreach ($vmhost in $($cluster | Get-VMHost)) { 

^^ that will return an array of VMhost objects and let you to proceed. (I'm away from my environment, so that may not be the exact syntax)

Reply
0 Kudos
tdubb123
Expert
Expert
Jump to solution

nice. that was it. Thanks

But report take a while to run through a large environment.

Would it be faster to use get-view instead of esxcli?

havent tried yet

Reply
0 Kudos
tdubb123
Expert
Expert
Jump to solution

I have some hosts that are in datacenters that dont have a cluster. Its a standalone host in a remote office(datacenter)

so my problem is when I do the cluster loop, it fails to grab the esx hosts that are not in a cluster (remote Offices) any idea what to . do?

$results= @()

$vmhosts = get-vmhost | ? {$_.powerstate -eq "PoweredOn"}

$i = 0

foreach($dc in get-datacenter){

   foreach($cluster in get-cluster -location $dc) {

        foreach ($vmhost in (get-cluster -name $cluster | get-vmhost | ? {$_.powerstate -eq "PoweredOn"} )) {    

$i++

$esxcli = get-esxcli -VMHost $vmhost

$hostview = get-vmhost $vmhost | Get-View

write-progress -Activity "Processing $($i) of $($vmhosts.count) " -CurrentOperation $vmhost -PercentComplete (($i/$vmhosts.count) * 100)

Start-sleep -Milliseconds 200

$info = "" | select esxHost, cluster, Datacenter, ESXVErsion, VendorNAme, Productname, SErialNumber, vcenter

$info.esxHost = $vmhost.name

$info.cluster = $cluster.name

$info.DAtacenter = $dc.name

$info.ESXVErsion = $hostview.config.product.fullname

$info.VendorName = $esxcli.hardware.platform.get().VendorName

$info.Productname = $esxcli.hardware.platform.get().Productname

$info.Serialnumber = $esxcli.hardware.platform.get().Serialnumber

$info.vcenter = $cluster.Uid.Split(":")[0].split("@")[1]

$results += $info

}}}

$results | Out-GridView

Reply
0 Kudos