Share via


ADSISearcher constructor

Question

Tuesday, June 28, 2011 1:28 PM

how can I build new searcher with providing the search root and filter? I tried this, but no luck..

$searcher = [adsisearcher] $objroot "(objectCategory=computer)"

tried with a , and with () and wasn’t able to construct the object with more than one param.
 also, just a basic AD question, why do you need to load the properties? I've always been able to access everything via $obj.properties.name or whatever. Is there a performance reason or is PS doing some magic in the back end to help me out?

Thanks
Justin

All replies (8)

Tuesday, June 28, 2011 1:45 PM ✅Answered

New-Object adsisearcher([adsi]"LDAP://Ou=Test,dc=contoso,dc=com","(objectCategory=computer)")


Tuesday, June 28, 2011 5:03 PM ✅Answered

When you create an object that way, you're using the default parse method of the object, and limited to what that method is expecting for input and what it's going to do with it.  For this object type, it appears to be expecting a string argument that will be used as the filter property.  It's taking the $objroot string and making it the filter property of the searcher, and then you get a parse error on the filter string and the searchroot never gets set.

$objroot ='[adsi]"LDAP://ou=Test,dc=contoso,dc=com"'
$searcher = [adsisearcher] $objroot # "(objectCategory=computer)"
$searcher | fl

 

CacheResults             : True
ClientTimeout            : -00:00:01
PropertyNamesOnly        : False
Filter                   : [adsi]"LDAP://ou=Test,dc=contoso,dc=com"
PageSize                 : 0
PropertiesToLoad         : {}
ReferralChasing          : External
SearchScope              : Subtree
ServerPageTimeLimit      : -00:00:01
ServerTimeLimit          : -00:00:01
SizeLimit                : 0
SearchRoot               : System.DirectoryServices.DirectoryEntry
Sort                     : System.DirectoryServices.SortOption
Asynchronous             : False
Tombstone                : False
AttributeScopeQuery      :
DerefAlias               : Never
SecurityMasks            : None
ExtendedDN               : None
DirectorySynchronization :
VirtualListView          :
Site                     :
Container                :

[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


Tuesday, June 28, 2011 1:53 PM

so you cant cast with two params?


Tuesday, June 28, 2011 2:34 PM | 2 votes

$searcher = [adsisearcher]"(objectCategory=computer)"
$searcher.searchRoot = [adsi]"LDAP://ou=foo,dc=domain,dc=com"
$searcher.PropertiesToLoad.AddRange(('name','samaccountname','cn'))

 

Or use DirectorySearcher directly:

New-Object System.DirectoryServices.DirectorySearcher (([adsi]"LDAP://ou=foo,dc=domain,dc=com"), '(objectCategory=computer)', ('cn','samaccountname','name'))

Shay Levy [MVP]
PowerShay.com
PowerShell Toolbar


Tuesday, June 28, 2011 2:41 PM

Thanks for the response Shay, the question was more so, how can I create the object via the type with two arguments.. which, looks like it cant be done so im leaning towards Kazun's method...

$computers = (new-object adsisearcher($objroot, "(objectCategory=computer)")).FindAll() | %{$_.properties.name}

and the properties to load was more of a, do I need to do that? I typically don’t use that and just access what I want via the .properties, wasn’t sure if loading the properties I knew I'd be using some how helped.

Thanks

Justin Rich http://jrich523.wordpress.com Please remember to mark the replies as answers if they help and unmark them if they provide no help.


Tuesday, June 28, 2011 3:39 PM

Something like this?

 

$objroot =[adsi]"LDAP://ou=Test,dc=contoso,dc=com"

$searcher = iex @"
new-object adsisearcher([adsi]"$($objroot.path)","(objectCategory=computer)")
"@

$searcher.findall()

[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


Tuesday, June 28, 2011 4:10 PM

no, basically I was trying to do it without new-object...

for example, this is ok

$searcher = [adsisearcher] "(objectcategory=computer)"

creating an object with a type shortcut sending one argument to the constructor....  but, why cant I send two arguments when creating an object this way?

$objroot = [adsi] "LDAP://DC=domain,DC=com"
$searcher = [adsisearcher]  $objroot "(objectcategory=computer)"

Unexpected token '(objectcategory=computer)' in expression or statement.

I've tried to group it a few ways, to tell it that the string, "(objectcategory=computer)" needs to be used with the constructor, but I cant find a way without using new-object as Kazun did.

why can only one argument be sent when creating objects this way?

Thanks

Justin Rich http://jrich523.wordpress.com Please remember to mark the replies as answers if they help and unmark them if they provide no help.


Tuesday, June 28, 2011 6:38 PM

it seems to assume all strings are filters, but it will happily take a DE and make it the object root.  you'll see it sets the filter to the default (objectclass=*) and the searchroot is set to the the DE passed to it.

so the parser looks at the type, just not the info in the type, and doesn’t deal with more than one argument..

wonder if it’s a .net thing or more so a powershell thing..

ahh well, I'll just take it as it is.

Thanks

Justin Rich http://jrich523.wordpress.com Please remember to mark the replies as answers if they help and unmark them if they provide no help.