VMware Cloud Community
tdubb123
Expert
Expert

datacenter location for vmhosts

any idea how I can get the datacenter location on these vmhosts?

$results = @()

$vmhosts = get-vmhost (get-content "c:\scripts\esxhosts.txt")

foreach($dc in get-datacenter){

foreach ($vmhost in $vmhosts) {

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

$result = ""|  select vmhostname, Version, Build, datacenter, vcenter

$result.vmhostname = $vmhost.name

$result.Version = $hostview.config.Product.Fullname

$result.Build = $hostview.config.Product.Build

$result.datacenter = $dc.name

$result.vcenter = $dc.Uid.Split(":")[0].Split("@")[1]

$results += $result

}}

$results | Out-GridView

It is showing all my hosts instead of hosts from the txt file

0 Kudos
4 Replies
LucD
Leadership
Leadership

You're running running throuh all the hosts for each datacenter.

Try like this

$results = @()

$vmhosts = Get-VMHost (Get-Content "c:\scripts\esxhosts.txt")

foreach ($vmhost in $vmhosts) {

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

    

    $result = ""|  select vmhostname, Version, Build, datacenter, vcenter

    $result.vmhostname = $vmhost.name

    $result.Version = $hostview.config.Product.Fullname

    $result.Build = $hostview.config.Product.Build

    $result.datacenter = (Get-Datacenter -VMHost $vmhost).Name

    $result.vcenter = $dc.Uid.Split(":")[0].Split("@")[1]

    $results += $result

}

$results | Out-GridView


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

0 Kudos
tdubb123
Expert
Expert

Thanks Luc. But vcenter is not reporting correctly

0 Kudos
tdubb123
Expert
Expert

ok looks like i got it working

$results = @()

$vmhosts = Get-VMHost (Get-Content "c:\scripts\esxhosts.txt")

foreach ($vmhost in $vmhosts) {

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

   

    $result = ""|  select vmhostname, Version, Build, datacenter, vcenter

    $result.vmhostname = $vmhost.name

    $result.Version = $hostview.config.Product.Fullname

    $result.Build = $hostview.config.Product.Build

    $result.datacenter = (Get-Datacenter -VMHost $vmhost).Name

    $result.vcenter = (Get-Datacenter -VMHost $vmhost).Uid.Split(":")[0].Split("@")[1]

    $results += $result

}

$results | Out-GridView

0 Kudos
LucD
Leadership
Leadership

You can save one Get-Datacenter, like this

$results = @()

$vmhosts = Get-VMHost (Get-Content "c:\scripts\esxhosts.txt")

foreach ($vmhost in $vmhosts) {

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

    $dc = Get-Datacenter -VMHost $vmhost

    $result = ""|  select vmhostname, Version, Build, datacenter, vcenter

    $result.vmhostname = $vmhost.name

    $result.Version = $hostview.config.Product.Fullname

    $result.Build = $hostview.config.Product.Build

    $result.datacenter = $dc.Name

    $result.vcenter = $dc.Uid.Split(":")[0].Split("@")[1]

    $results += $result

}

$results | Out-GridView


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

0 Kudos