user911810
user911810

Reputation:

POWERSHELL - Call Operator - What does this structure mean?

this code runs well. It was copied from Microsoft.

$strFilter = "(&(objectCategory=person)(objectClass=user))" 

$objDomain = New-Object System.DirectoryServices.DirectoryEntry 

$objSearcher = New-Object System.DirectoryServices.DirectorySearcher 
$objSearcher.SearchRoot = $objDomain 
$objSearcher.PageSize = 1000 
$objSearcher.Filter = $strFilter 

$colProplist ="Name"
foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)} 

$colResults = $objSearcher.FindAll() 

foreach ($objResult in $colResults) 
   {$objItem = $objResult.Properties; $objItem.name }

However, I did not understand why the teacher have used

 $strFilter = "(&(objectCategory=person)(objectClass=user))" 

because all information I have got from web were this symbol & is for execute a code, once its meaning is "Call Operator".

I think that, in this case, it has an other meaning.

Upvotes: 2

Views: 88

Answers (2)

Buxmaniak
Buxmaniak

Reputation: 478

We talking about LDAP filter, take a look to Microsoft MSDN for further information. Your example is also discussed there.

Or take this way to get help inside Powershell:

Import-Module ActiveDirectory
Get-Help about_ActiveDirectory_Filter

... or online here

Upvotes: 1

hjpotter92
hjpotter92

Reputation: 80647

Compound search/filter expression

(&(objectCategory=person)(objectClass=user))

is equivalent to the following conditional in programming language context:

objectCategory == person && objectClass == user

It is documented on MSDN page for DirectorySearcher.Filter Property:

Compound expressions are formed with the prefix operators & and |. An example is "(&(objectClass=user)(lastName= Davis))". Another example is "(&(objectClass=printer)(|(building=42)(building=43)))".

So, the filter is applied as:

objectClass=user && lastName= Davis

and

objectClass=printer && ( building=42 || building=43)

respectively, in the snippet provided.

Upvotes: 3

Related Questions