locked
Strange behavior of syntax RRS feed

  • Question

  • hi,

    I have a weird result from this syntax :

    This does not work

    Get-ADUser -Filter { UserPrincipalName -eq $user.UPN }

    This works

    $upn = $user.UPN
    Get-ADUser -Filter { UserPrincipalName -eq $upn}

    Does it mean that I cannot parsing things directly in the filter and I have to prepare exactly what I would like to search for in advance ?


    The key of learning is practice.


    Friday, October 27, 2017 11:58 AM

Answers

  • Thank you for the effort, but I found the issue.

    It is a beginner issue, my god, shame on me. I import the upn but I don't do any Trim() on the field to be sure it is what it shows without spaces at sides. In this case the variables contain a space at the end. This is why it didn't work.


    The key of learning is practice.

    • Marked as answer by arnaud.helin Friday, November 3, 2017 12:52 PM
    Friday, November 3, 2017 12:52 PM

All replies

  • Get-AdUser -Filter "UserPrincipalName -eq '$($user.UPN)'"


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    Friday, October 27, 2017 12:12 PM
  • UPN is not a standard attribute of the AD. This should work:
    Get-AdUser -Filter "UserPrincipalName -eq '$($user.UserPrincipalName)'"
    


    Best regards (79,108,97,102|%{[char]$_})-join''

    Friday, October 27, 2017 12:15 PM
  • Hi,

    @clayman2: this syntax doesn't change the problem. I already tried it.

    @BOfH_666: $user is my PSCustomObject wich contains 3 properties. The property UPN is one of those. By the way, if $var is a string, doing: <field> eq $var or <field> eq "$($var)" or <field> eq $var.ToString(), are exactly the same (in the absolute).

     

    To avoid misunderstanding, my UPN property is already a string. If I import only one of the properties from my  CSV file, specifying -ExpandProperty (which means directly the value, to be brief), it works like a charm. But I can't do that. I need the others properties.

     


    The key of learning is practice.

    Friday, October 27, 2017 12:32 PM
  • The parameter is parsed as string instead as scriptblock, hence dot-Notation of properties is not working. 
    Friday, October 27, 2017 12:35 PM
  • So you might have provided us with a little more of the code you're using. I just tried it and it works just as expected for me. Here is what I used:
    Get-ADUser -Filter * | Export-Csv -Path C:\Files\Work\users.csv -Encoding UTF8 -Delimiter ';' -NoTypeInformation -Append
    Import-Csv -Delimiter ';' -Path C:\Files\Work\users.csv |
        ForEach-Object {
            Get-ADUser -Filter "UserPrincipalName -eq '$($_.UserPrincipalName)'"
        }


    Best regards (79,108,97,102|%{[char]$_})-join''

    Friday, October 27, 2017 12:45 PM
  • Hi,

    My CSV contains 3 columns, Firstname, Lastname, UPN.

    Here my code (I removed useless things):

    $csv = Import-Csv -Path C:\Users\heliar01\Desktop\UPN.csv -Delimiter ';' | Select-Object -Property Firstname,Lastname,UPN
    
    foreach($user in $csv)
    {
      #$upn = $user.UPN
      if(Get-ADUser -Filter { UserPrincipalName -eq $user.UPN } -ErrorAction Ignore)
      {
        #Action A
      }
      else
      {
        #Action B
      }
    }


    The key of learning is practice.



    Friday, October 27, 2017 2:12 PM
  • Import-Csv -Path C:\Users\heliar01\Desktop\UPN.csv -Delimiter ';' | foreach {
      #$upn = $user.UPN
      if(Get-ADUser -Filter "UserPrincipalName -eq '$($_.UPN)'")
      {
        #Action A
      }
      else
      {
        #Action B
      }
    }
    Try it like this

    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''


    • Edited by clayman2 Friday, October 27, 2017 2:23 PM typo
    Friday, October 27, 2017 2:17 PM
  • When using a "-Filter" "ErrorAction" is unnecessary.


    \_(ツ)_/

    Friday, October 27, 2017 2:19 PM
  • @clayman2: it works sure but the thing is I would like to know why it works with this syntax or by making variable just before the request, instead of -Filter { } syntax.

      

    @jrv: thank you, I used this in another script with Test-Path. I didn't know that the Filter remove the need.

     

    The key of learning is practice.

    Friday, October 27, 2017 2:29 PM
  • Here is how to extend the use of a filter:

    if($user = Get-ADUser -Filter "UserPrincipalName -eq '$($_.UPN)'")
          # user found
           Write-Host $user.Name
    }else{
         # user not found
    }
    



    \_(ツ)_/

    Friday, October 27, 2017 2:32 PM
  • Thank you for the effort, but I found the issue.

    It is a beginner issue, my god, shame on me. I import the upn but I don't do any Trim() on the field to be sure it is what it shows without spaces at sides. In this case the variables contain a space at the end. This is why it didn't work.


    The key of learning is practice.

    • Marked as answer by arnaud.helin Friday, November 3, 2017 12:52 PM
    Friday, November 3, 2017 12:52 PM