Question re ADSISearcher and early filtering

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
EBrant
Posts: 99
Joined: Thu Feb 02, 2012 1:51 am

Question re ADSISearcher and early filtering

Post by EBrant » Thu Mar 07, 2013 1:46 am

Hello All

Can someone please help me with the following question please.
I want a acheive early filtering using ADSISearcher, please see below

#works
([DirectoryServices.DirectorySearcher]"objectCategory=Computer").findone().psbase.properties

# does not work i.e. returns nothing
([DirectoryServices.DirectorySearcher]"(&(objectCategory=Computer)(dnshostname =W2K3*))").findone().psbase.properties

# returns dnshostname, however this is late filtering
([DirectoryServices.DirectorySearcher]"objectCategory=Computer").findone().psbase.properties | % {$_.dnshostname -match "^W2K3"}

Now I am thinking is this just the way it is? or can I filter on dnshostname and objectCategory before I send down the pipeline, it occurs to me there must be a way?

Thanks all in advance
Ernie

User avatar
jvierra
Posts: 13792
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Question re ADSISearcher and early filtering

Post by jvierra » Thu Mar 07, 2013 2:27 am

Perhaps a little simplification would help:
PowerShell Code
Double-click the code block to select all.
#
([adsisearcher]'objectcategory=computer').FindOne().properties
#
'psbase' is not needed.

What is 'early filtering'. Thee is no such thing. Perhaps you are talking about using a WMI filter instead of aPowerShell fiter (where-object).

dnshostname is unique so you do not need anything else.
PowerShell Code
Double-click the code block to select all.
#
([adsisearcher]'dnshostname=W2K3*').FindOne().properties
#
However - 'properti3es' are not what you think they are and require very specual handling as they are all wrapped in collections.

Here is a link to an example of how to use [adsisearcher] and how to unwrap the properties:

http://gallery.technet.microsoft.com/sc ... f-6f59d3b4

User avatar
EBrant
Posts: 99
Joined: Thu Feb 02, 2012 1:51 am

Re: Question re ADSISearcher and early filtering

Post by EBrant » Thu Mar 07, 2013 11:20 am

Hello Jim, thanks very much again for your help,

That's right what I meant by early filering is like with WMI i.e. I wanted to get ADSI to reduce the amount of data returned in the first instance before I send down the pipeline to where-object.

Thanks again I will check out the Link

Ernie

Locked