Solved

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

Posted on 2016-11-09
4
28 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 85

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 85

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

After seeing many questions for JRNL_WRAP_ERROR for replication failure, I thought it would be useful to write this article.
Let's recap what we learned from yesterday's Skyport Systems webinar.
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 …
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Suggested Courses

622 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