Get-ADUser errors out weirdly

This topic contains 8 replies, has 3 voices, and was last updated by  Richard Diphoorn 1 year, 7 months ago.

  • Author
    Posts
  • #37359
    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...

  • #37360

    Richard Diphoorn
    Participant

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

    Anyway, try this:

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

  • #37363

    🙂
    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

  • #37364

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

    Thanks!

  • #37365

    Richard Diphoorn
    Participant

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

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

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

    '$a.txt'
    $a.txt
  • #37367

    Dave Wyatt
    Moderator

    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. 🙂

  • #37368

    omg. yeah, 3 hours totally wasted.

    Thanks folks! case closed!

  • #37369

    Richard Diphoorn
    Participant

    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.

You must be logged in to reply to this topic.