Solved

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

Posted on 2016-11-09
4
25 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 84

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 84

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

Featured Post

Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
Active Directory security has been a hot topic of late, and for good reason. With 90% of the world’s organization using this system to manage access to all parts of their IT infrastructure, knowing how to protect against threats and keep vulnerabil…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

740 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