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
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)
Which error?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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
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)
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
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