Hetfield84
Enthusiast
Enthusiast

Get which vCenter a host is connected to

Hi,

I'm running a script to pull ESXi build numbers from hosts across all of vCenters. Is there a way to get what vCenter a host is connected to, so I can add that to my output?

Get-VMHost | ForEach-Object -Process { 
  [PSCustomObject]@{
    'lastpatchdate' = [datetime]((Get-ESXCli -VMHost $_).software.vib.list() |
      Select-Object -Property installdate -ExpandProperty installdate |
      Sort-Object -Descending)[0]
    'host'        = $_.Name
    'version'     = $_.version
    'build'       = $_.build
  } 
} | export-csv "$PSScriptRoot\patches.csv" -NoTypeInformation

Get-VMHost | ForEach-Object -Process { 

  [PSCustomObject]@{

    'lastpatchdate' = [datetime]((Get-ESXCli -VMHost $_).software.vib.list() |

      Select-Object -Property installdate -ExpandProperty installdate |

      Sort-Object -Descending)[0]

    'host'        = $_.Name

    'version'     = $_.version

    'build'       = $_.build

  } 

} | export-csv "$PSScriptRoot\patches.csv" -NoTypeInformation

0 Kudos
2 Replies
LucD
Leadership
Leadership

Try like this

Get-VMHost | ForEach-Object -Process {

  [PSCustomObject]@{

    'lastpatchdate' = [datetime]((Get-ESXCli -VMHost $_).software.vib.list() |

      Select-Object -Property installdate -ExpandProperty installdate |

      Sort-Object -Descending)[0]

    'host'        = $_.Name

    'version'     = $_.version

    'build'       = $_.build

    'vCenter'     = ([uri]$_.ExtensionData.Client.ServiceUrl).Host

  }

} | export-csv "$PSScriptRoot\patches.csv" -NoTypeInformation


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

0 Kudos
Hetfield84
Enthusiast
Enthusiast

That worked perfectly. Thank you!
0 Kudos