Tuesday, May 13, 2014

Leveling an LDAP Query String

The neat freak in me likes to level strings in code to make them easier to read.  This is just a little trick to level a string containing an LDAP filter before sending it to Get-ADUser.

The trick is to just level the string out, but remove the CR and LF using the –replace operator.

The LDAPFilter variable will then contain something usable by Get-ADUser while you still get to look at something easy to read.

 

$LDAPFilter = "

(&

    (mail=hoof@hearted.ca)

    (company=litware)

    (!userAccountControl:1.2.840.113556.1.4.803:=2)

    (memberof:1.2.840.113556.1.4.1941:=CN=Shellers,OU=Groups,DC=Litware,DC=ca)

)" -replace "`r" -replace "`n"

 

Get-ADUser -LDAPFilter $LDAPFilter

 

No comments: