Good morning!
Hopefully I can explain the problem I’m struggling with. Attempting to apply the $base variable to the Get-ADComputer cmdlet in the $servers variable. Each attempt produces the following error:
Get-ADComputer : The supplied distinguishedName must belong to one of the following partition(s): ‘CN=Configuration,DC=nl,DC=com , CN=Schema,CN=Configuration,DC=nl,DC=com ,
DC=nlong,DC=com , DC=ForestDnsZones,DC=nl,DC=com , DC=DomainDnsZones,DC=nlong,DC=com’.
At line:1 char:12
- $servers = Get-ADComputer -Filter “OperatingSystem -like ‘*’” -Search …
The second issue is the ou structure we have, please note the $ou and $ou1. Would like to combine the output of both variables into my $servers variable which feeds into the $result for each loop.
Any and all help would be greatly appreciated!
Norm
HERE IS MY SCRIPT:
Importing Active Directory Module
Import-Module ActiveDirectory
Set variable which defines the ou we will search for windows servers
#$ou = “OU=HQ,OU=Servers,DC=nlong,DC=com”
#$ou = “OU=Auto Restart 2300 Prod,OU=HQ,OU=Servers,DC=nlong,DC=com”
#$ou1= “OU=Auto Restart 2300 NonProd,OU=HQ,OU=Servers,DC=nlong,DC=com”
#$ouName = “OU=Auto Restart 2300,OU=NonProd,OU=HQ,OU=Servers,DC=nlong,DC=com”
$base = Get-ADOrganizationalUnit -Filter ‘Name -like “2300prod*”’ | Select-Object DistinguishedName
#$base = Get-ADOrganizationalUnit -Filter “Name -like ‘$ouName’”
#Find all windows servers in $ouName’
$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
}