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
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
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
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