VMware Cloud Community
nlong12
Enthusiast
Enthusiast
Jump to solution

Get-Vm foreach loop issue

Good afternoon fellow powershell users;

I'm attempting to get this snippet of code to work

#Find all windows servers in $ou

$servers = Get-ADComputer -Filter {OperatingSystem -like '*'} -SearchBase $ou | select-object Name | sort

#Using a foreach loop to find powered on VM's in the $ou variable.  Here were simply listing the vm's to be powered down

$result = foreach ($server in $servers) {

Get-VM -Name $server | where { ($_.PowerState -eq "PoweredOn") } | Select-Object -ExpandProperty Name | Sort

}

Can't seem to get get-vm -Name $server to work, this should be simple but I'm drawing a blank.

Thank you for your input

Norm

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try like this

$ouName = 'SomeOU'

$base = Get-ADOrganizationalUnit -Filter "Name -like '$ouName'"

#Find all windows servers in $ou

$servers = Get-ADComputer -Filter "OperatingSystem -like '*'" -SearchBase $base | select-object -ExpandProperty Name | sort

#Using a foreach loop to find powered on VM's in the $ou variable.  Here were simply listing the vm's to be powered down

$result = foreach ($server in $servers) {

    Get-VM -Name $server | where { ($_.PowerState -eq "PoweredOn") } | Select-Object -ExpandProperty Name | Sort

}


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

View solution in original post

Reply
0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

Try like this

$ouName = 'SomeOU'

$base = Get-ADOrganizationalUnit -Filter "Name -like '$ouName'"

#Find all windows servers in $ou

$servers = Get-ADComputer -Filter "OperatingSystem -like '*'" -SearchBase $base | select-object -ExpandProperty Name | sort

#Using a foreach loop to find powered on VM's in the $ou variable.  Here were simply listing the vm's to be powered down

$result = foreach ($server in $servers) {

    Get-VM -Name $server | where { ($_.PowerState -eq "PoweredOn") } | Select-Object -ExpandProperty Name | Sort

}


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

Reply
0 Kudos
nlong12
Enthusiast
Enthusiast
Jump to solution

Hello Lucd;

As always thank you for response!!   Due the our unique AD layout hear is what I came up with.

# Set $base variable variable which defines the ou we will search for servers

$bases2300 = Get-ADxxanizationalUnit -Filter 'Name -like "*"' -SearchBase 'OU=Auto Restart 2300 Prod,OU=HQ,OU=Servers,DC=xxxx,DC=xx' -SearchScope OneLevel | Select-Object -ExpandProperty distinguishedname

#Find all servers in the $base variable

$servers2300 = foreach ($base2300 in $bases2300) {

  Get-ADComputer -Filter "OperatingSystem -like '*'" -SearchBase $base2300 | select-object -ExpandProperty Name | sort

}

#Using a foreach loop to find powered on VM's in the $ou variable.  Here were simply listing the vm's to be powered down

$result2300 = foreach ($server2300 in $servers2300) {

    Get-VM -Name $server2300 | where { ($_.PowerState -eq "PoweredOn") } | Select-Object -ExpandProperty Name | Sort

}

$result2300 | Out-file -filepath D:\powershell_temp\2300VMs.txt

Thanks again

Norm

Reply
0 Kudos