Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Powershell ADSI Subtree Search

Posted on 2007-10-01
10
Medium Priority
?
3,584 Views
Last Modified: 2008-01-09
How can I set the search scope to the entire subtree in this powershell script.

==================================================================
$domain = [ADSI]"LDAP://dc=domain_name,dc=com"
$users = $domain.psbase.children | where-object {$_.objectClass -match "user"}
==================================================================

I know there will be many ways to do this but I want to know the basic way as I am trying to understand and learn powershell.

Thanks
0
Comment
Question by:Nael_Shahid
  • 6
  • 4
10 Comments
 
LVL 18

Expert Comment

by:BSonPosh
ID: 19990101

What you want to use is DirectorySearcher. I assume your wanting all users.. try this

$dom = [ADSI]"LDAP://dc=corp,dc=bb,dc=lab"
$filter = "(&(objectcategory=user))"
$ds = new-object System.DirectoryServices.DirectorySearcher($dom,$filter)
$users = $ds.Findall()
$users

here is this in one line
$users = (new-object System.DirectoryServices.DirectorySearcher([ADSI]"LDAP://10.254.254.1/dc=corp,dc=bb,dc=lab","(&(objectcategory=user))")).findall()
0
 
LVL 18

Accepted Solution

by:
BSonPosh earned 1000 total points
ID: 19990112
whoops... the one liner should be like

$users = (new-object System.DirectoryServices.DirectorySearcher([ADSI]"LDAP://dc=corp,dc=bb,dc=lab","(&(objectcategory=user))")).findall()

I have a VM and I have to use the IP... you should be able to just resolve it.

Also note: if your in the domain you can use [ADSI]"" instead of [ADSI]"LDAP://dc=corp,dc=bb,dc=lab"
0
 

Author Comment

by:Nael_Shahid
ID: 19990509
Thanks for this.

Do you mind giving me a brief explanation of what is going on in this code?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 18

Expert Comment

by:BSonPosh
ID: 19990585
no problem. You can check my blog for more examples I deal with ADSI alot.

# This gets a directory entry for the domain
$dom = [ADSI]"LDAP://dc=corp,dc=bb,dc=lab"

# This is a string the represents the LDAP filter you want to pass
$filter = "(&(objectcategory=user))"

# This is creating a new directorySearcher object with the domain and filter to search
$ds = new-object System.DirectoryServices.DirectorySearcher($dom,$filter)

# This is executing the Searcher and putting results in $users
$users = $ds.Findall()

# this is outputing the users
$users


side note... if you have more than a 1000 users you need to add a pagesize value on the searcher like this
$ds.pagesize = 1000

This will go before the .findall()

Is this clear? Please let me know.
0
 

Author Comment

by:Nael_Shahid
ID: 19990695
Yes this is a lot clearer thanks.

One question; in $filter where you use an ldap query, could you have used something like the Where-object {$_.objectClass -match "user"}?

The reason I ask is because I think I understand this way of doing things. I dont know how to write an LDAP query. Basically I want to learn how to use Powershell with AD by doing simple tasks first and building from there, but the introduction of LDAP queries seems to make it harder to learn?..    
0
 
LVL 18

Expert Comment

by:BSonPosh
ID: 19990783
"One question; in $filter where you use an ldap query, could you have used something like the Where-object {$_.objectClass -match "user"}?"
Quick Answers: No

Slightly Longer Answer:
Where-object is a cmdlet that allows you to filter the results you get back, but it still parses the info.
and Ldap filter is parsed on the server side so you only get back the objects that match your filter (WAY FASTER.)

In this case you would have to dump all of AD just to find users... much better to have AD only return the user objects.
0
 

Author Comment

by:Nael_Shahid
ID: 19991295
Hi - For some reason I can not get the script to work. When creating a script file with the above code I do not get any results.
0
 

Author Comment

by:Nael_Shahid
ID: 19991803
Managed to get this working.

I was running the script then once it was finished I was then trying to list the objects by typing the variable but the variable didn't exist. Can you confirm the variable does not remain in the console once the script has executed?
0
 
LVL 18

Expert Comment

by:BSonPosh
ID: 19997909
That is correct.. unless you capture the scripts output like

$results = c:\myscript.ps1
0
 
LVL 18

Expert Comment

by:BSonPosh
ID: 19997932
May I suggest some reading... Keith Hill has a awesome series on his blog one of which explains how powershell deals with output.

http://keithhill.spaces.live.com 
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Organizations create, modify, and maintain huge amounts of data to help their businesses earn money and generally function.  Typically every network user within an organization has a bit of disk space to store in process items and personal files.   …
On July 14th 2015, Windows Server 2003 will become End of Support, leaving hundreds of thousands of servers around the world that still run this 12 year old operating system vulnerable and potentially out of compliance in many organisations around t…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…

569 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