Issues with Get-ADOrganizationalUnit and Get-ADComputer

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
}

Please edit your post and format the code as code. This way it’s almost impossible to copy and to debug it. I would recommend as well to remove all unnecessary comments you placed there for your testing or developing.

Hello Olaf;
Cleaned up my code, please let me know if it isn’t readable!

Thank you!

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

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


$base = Get-ADOrganizationalUnit -Filter 'Name -like "*2300*prod*"' | Select-Object DistinguishedName 

 
#Find all windows servers in $base  
$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
} 

Actually I meant to edit your original post but anyway …
You should check what’s in your variable $Base. It should be only the name of the OU. You might change the assignment of your variable to this:

$base = Get-ADOrganizationalUnit -Filter ‘Name -like “2300prod*”’ | Select-Object -ExpandProperty DistinguishedName 

Hello Olaf;
Your suggestion solved my problem

Thank you

Norm