Freigeben über


AD Cmdlet -Filter Faffing

I'm aware that I write for an international audience and I try to use international English in my posts.

 

When I use traditional or colloquial English I usually have a good reason for doing so. Today's reason is because I like the alliteration that 'Filter Faffing' provides.

 

faffing - the act of making a task more awkward or time-consuming than it ought to be...  

 

For those of you who've worked with the Active Directory PowerShell module over the years, you'll know that it has its idiosyncrasies.

 

Arriving at the correct syntax for the -Filter parameter can sometimes be fun. If you check out the help entry for the -Filter option the returned information can be a little impenetrable because of the use of Backus-Naur notation.

 

 
man Get-ADComputer -Parameter filter

 

 

 

Someone was on their fourth coffee when writing documentation that day! In fact, their finger prints are over a lot of the module documentation...

 

 
get-help Backus

 

 

Sifting through the Backus-Naur notation, you soon realise that the -Filter parameter should accept something like standard PowerShell syntax… sometimes, though, the syntax is nothing like what you'd expect.

 

Consider this…

 

 
$param = "unn"
Get-Service | ? {$_.status -like "*$param*"} | Select-Object -First 5

 

 

All good, so far. Now, consider this…

 

 
$paramm = "arr"
Get-Aduser -Filter {SamAccountName -like "*$paramm*"}

 

 

Odd. And, now consider this…

 

 
$paramm = "arr"
Get-Aduser -Filter "SamAccountName -like '*$Paramm'"

 

 

"Curiouser and curiouser!", cried PoSh Chap (he was so much surprised, that for the moment he quite forgot how to speak traditional English), "Definitely, one to look out for!"

Comments

  • Anonymous
    August 25, 2017
    Michael Klement has an excellent explanation on the behavior on Stack Overflow: https://stackoverflow.com/questions/20075502/get-aduser-filter-will-not-accept-a-variable/44184818#44184818
    • Anonymous
      August 26, 2017
      Nice! & thanks for sharing!
  • Anonymous
    August 28, 2017
    also it looks like you have a typo you show $paramm = "arr"Get-Aduser -Filter {SamAccountName -like "$arr"}but the powershell screen shows$paramm = "arr"Get-Aduser -Filter {SamAccountName -like "$param"}
    • Anonymous
      August 31, 2017
      Thanks, Tony. Post updated.
      • Anonymous
        September 01, 2017
        btw keep up the good work.
        • Anonymous
          September 01, 2017
          Mostly on topic, I have another strange one within ps, I have ps1 with all (3) of my exit statements commented out and it still exits :)