Question re ADSISearcher and early filtering

Ask your 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.
This topic is 11 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
EBrant
Posts: 99
Last visit: Wed Apr 23, 2014 2:22 am

Question re ADSISearcher and early filtering

Post by EBrant »

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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Question re ADSISearcher and early filtering

Post by jvierra »

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
Last visit: Wed Apr 23, 2014 2:22 am

Re: Question re ADSISearcher and early filtering

Post by EBrant »

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
This topic is 11 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked