Author Posts

April 7, 2016 at 5:47 am

Get-ADUser -Filter {samaccountname -eq $_.Samaccountname}
Get-ADUser : Error parsing query: 'samaccountname -eq $_.Samaccountname' Error Message:
'syntax error' at position: '20'.

I tried various things with filter but it will only return errors like this or returns null. Any ideas? I can't seem to figure it out. I tried tracing and using ISE to debug, but I can't understand why it won't return anything...

April 7, 2016 at 6:04 am

Did you tried Googling that? 😉 😛 hehehe....

Anyway, try this:

Get-ADUser -Filter "SamAccountName -eq '$($_.SamAccountName)'"

April 7, 2016 at 6:16 am

🙂
Yeah, I've tried, and I'm still trying and its been 3 hours already that's why I posted this really stupid question... but to the point.

Get-ADUser -Filter "samaccountname -eq $($a.txt)"
Get-ADUser : Error parsing query: 'samaccountname -eq palaginrv' Error Message: 'syntax error' at position: '20'.

What is that supposed to mean? O_o

April 7, 2016 at 6:20 am

Oh wait, I forgot ' '
but I thought that anything inside ' ' gets treated like text not expressions?

Thanks!

April 7, 2016 at 6:22 am

Put single quotes around $($a.txt) like this:

Get-ADUser -Filter "samaccountname -eq '$($a.txt)'"

April 7, 2016 at 6:23 am

Yeah, thanks! But why does it work that way? where do I read about that? 😉

'$a.txt'
$a.txt

April 7, 2016 at 6:25 am

That whole thing is inside a double-quoted string:

"'$($a.txt)'"

So in this case, the single-quotes are just characters in the string; they have no special meaning to PowerShell.

However, on the inside of the AD functions, it's taking that string an evaluating it as code. So the string you want to produce is:

SamAccountName -eq 'SomeValue'

Long story short, the AD commands are weird and annoying. 🙂

April 7, 2016 at 6:38 am

omg. yeah, 3 hours totally wasted.

Thanks folks! case closed!

April 7, 2016 at 6:39 am

The filter option is a bit wacky. The filter uses PowerShell Expression Language. If you look at the help, you see that the filter parameter takes in a string:

PS C:\> Get-Help Get-ADUser -Parameter Filter

-Filter 

    Required?                    true
    Position?                    Named
    Accept pipeline input?       false
    Parameter set name           Filter
    Aliases                      None
    Dynamic?                     true

So anything you will pass to the filter will be taken in as a string. To get the subexpression to work, so expanding the variable and return it as a string to the filter, you put these within single quotes.