Solved

Need a script to get user details from 2 different OU's with particular attribute values

Posted on 2016-11-09
4
20 Views
Last Modified: 2016-11-09
I've a requirement to get all user details from 2 different OU's. Part of the Job is to just get the user details with attribute "xyz" with value either "0" or "4" and another attribute "abc" with value "NULL" (empty or nothing).

OU from where i need to grab user details are below:

OU=Test1,DC=CONTOSO,DC=COm
OU=Test2,DC=CONTOSO,DC=COM

I was able to write below:

Get-aduser -filter "xyz -eq 4" -searchbase "OU=Test2,DC=CONTOSO,DC=COM" | select name,samaccountname,xyz

But I am not able to club both OU's together and i am not even sure if it's possible. Also i am not sure how to put value "0" for attribute "xyz" in the same script.

Let me know if any other info is required

Thanks in advance!!.
0
Comment
Question by:A D
  • 2
  • 2
4 Comments
 
LVL 83

Accepted Solution

by:
oBdA earned 500 total points
ID: 41880557
This should do the trick:
$SourceOUs = @(
	"OU=Test1,DC=CONTOSO,DC=COm"
	"OU=Test2,DC=CONTOSO,DC=COM"
)
$SourceOUs | ForEach-Object { 
	Get-ADUser -SearchBase $_ -Filter "((xyz -eq '0') -or (xyz -eq '4')) -and (abc -notlike '*')"
}

Open in new window

0
 

Author Comment

by:A D
ID: 41880676
Thanks oBdA. I appreciate it. I'll test your script as well but I was able to come up with my own version. It might help somebody else.

Get-ADUser -Filter xyz-eq 0)-or xyz-eq 4))-and (-not ( abc -like "*")) } -Properties * | ? { ($_.canonicalname -like "*Test1*") -or ($_.canonicalname -like "*Test2*")}

Thanks again!!!.
0
 

Author Closing Comment

by:A D
ID: 41880677
Thanks oBdA.
0
 
LVL 83

Expert Comment

by:oBdA
ID: 41880704
Your solution would query all AD users, filtering them by OU only in Powershell. That's unnecessary stress for the AD, since you already know that you have a restricted search base.
0

Join & Write a Comment

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
Find out how to use Active Directory data for email signature management in Microsoft Exchange and Office 365.
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now