Powershell question on Get-ADUser

I am modifying a script and running into an issue. I have to run get-aduser and query two seperate OUs. I can't seem to get the script to pass the info the my variable right for further processing. This is what I have thus far.

#Declare Variables
$HitachiGroupDN = "CN=Hitachi-ID Exclude,OU=Security,OU=Groups,DC=company,DC=com"
$TestAccountsGroupDN= "CN= Test Accounts,OU=Security,OU=Groups,DC=company,DC=com"
$ServiceAccountsGroupDN= "CN= Service Accounts,OU=Security,OU=Groups,DC=company,DC=com"
$WombatExemptionGroupDN= "CN=Wombat Report Exemption,OU=Security,OU=Groups,DC=company,DC=com"

#Search Locations
$ou = "OU=San Diego,OU=company,DC=company,DC=com","OU=boston,OU=company,DC=company,DC=com"


$results = Get-ADUser -SearchBase $ou -Filter { memberof -ne $HitachiGroupDN -and memberof -ne $TestAccountsGroupDN -and memberof -ne $ServiceAccountsGroupDN -and memberof -ne $WombatExemptionGroupDN -and enabled -eq 'True'} -Properties  mail,GivenName,sn,Department,WhenCreated

Open in new window


when I run, I get an error that states I am passing a string to the -searchbase arguement which is not allowed. How can I get the variable $results to have the query's filters above for 2 seperate OUs?

Thank you for the help!
LVL 1
jbla9028Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David SankovskySenior SysAdminCommented:
You almost got it right.

try this.

#Declare Variables
$HitachiGroupDN = "CN=Hitachi-ID Exclude,OU=Security,OU=Groups,DC=company,DC=com"
$TestAccountsGroupDN= "CN= Test Accounts,OU=Security,OU=Groups,DC=company,DC=com"
$ServiceAccountsGroupDN= "CN= Service Accounts,OU=Security,OU=Groups,DC=company,DC=com"
$WombatExemptionGroupDN= "CN=Wombat Report Exemption,OU=Security,OU=Groups,DC=company,DC=com"

#Search Locations
$oulist = "OU=San Diego,OU=company,DC=company,DC=com","OU=boston,OU=company,DC=company,DC=com" # create a string array object

Foreach ($ou in $oulist){ #run once for each member in the oulist array
$results = Get-ADUser -SearchBase $ou -Filter { memberof -ne $HitachiGroupDN -and memberof -ne $TestAccountsGroupDN -and memberof -ne $ServiceAccountsGroupDN -and memberof -ne $WombatExemptionGroupDN -and enabled -eq 'True'} -Properties  mail,GivenName,sn,Department,WhenCreated}
#proceed with result processing as needed

Open in new window


That way, you can add additional OUs to the list without any further changes to the code.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jbla9028Author Commented:
Perfect. Thank you!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.