admin
Immortal
Immortal

Variable for all hosts in a given datacenter

Hi All,

I am modifying a Powershell script and could use a hand.

Currently, a variable is declared that includes all hosts that are connected in a datacenter:

$DataCenterHosts = Get-VMHost | where {$_.state -eq "Connected"}

Unfortunately, my vCenter Server contains multiple data centers, so the variable includes ALL hosts connected to this vCenter Server.

I need to change this variable to include all hosts that are connected to a specific data center named DataCenter1.

Any suggestions?


Thanks,
Drew

0 Kudos
4 Replies
admin
Immortal
Immortal

I found part of the answer in the PowerCLI reference:

-Location (Get-Datacenter -Name DataCenter1)

Now I just need to connect this with the existing
Get-VMHost statement below.


Drew

0 Kudos
LucD
Leadership
Leadership

You have the answer, just connect the dots 🙂

Seriously, you can limit the Get-VMHost to only datacenter1 like this

$DataCenterHosts = Get-VMHost -Location (Get-Datacenter -Name datacenter1)


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

admin
Immortal
Immortal

LucD,

Thanks!  I actually had tried that before I left work for the day,  it did not work right away, but then I realized I had to add a "for each" loop to another part of the script.  I will try this again on Monday and report back.


Thanks !


Drew

0 Kudos
admin
Immortal
Immortal

This worked today.  I actually found another area in the script that needed to be fixed in addition to this one.


Thanks!

Drew

0 Kudos