PowerCLI

 View Only
  • 1.  collect hosts info accross multiple vCenters

    Posted Oct 18, 2022 01:29 PM

    hello all,

    We have multiple vCenters servers I'm trying is to collect some basic data (for start).

    PS C:\> foreach ($vc in $global:DefaultVIServers) {
    $esxi = get-vmhost -Server $vc
    
    [PSCustomObject]@{
    Name = $esxi.name
    ConnectionState = $esxi.ConnectionState
    PowerState = $esxi.PowerState
    Version = $esxi.Version
    }
    }

    I'm not sure why I get the data in  { }.

    Name                                                     ConnectionState
    ----                                                     ---------------
    {esx01.local, esx02.local, esx03.local, esx04.local} 	{Maintenance, Connected, Connected, Connected}
    {prod01.local, prod02.local, prod03.local, prod04.local}  {Connected, Connected}
    {dns01.local, dns02.local, dns03.local, dns04.local}      {Connected, Connected, Connected, Connected...}
    {dc01.local, dc02.local, dc03.local, dc04.local}  	{Connected, Connected, Connected, Connected...}
    {test01.local, test02.local, test03.local, test04.local} {Connected, Connected, Connected, Connected}

     am I missing something obvious here or I'm overthinking it?

    Thanks, Mike



  • 2.  RE: collect hosts info accross multiple vCenters

    Posted Oct 18, 2022 04:03 PM

    Your $esxi variable contains an array of ESXi nodes, hence all derived properties are also arrays (which the {} indicates)

    You can add another loop over all ESXi nodes.
    Something like this for example

    foreach ($vc in $global:DefaultVIServers) {
    
      Get-VMHost -Server $vc -PipelineVariable esxi |
      ForEach-Object -Process {
    
      [PSCustomObject]@{
        Name = $esxi.name
        ConnectionState = $esxi.ConnectionState
        PowerState = $esxi.PowerState
        Version = $esxi.Version
      }
    }
    }