Passing string in Get-ADUser filter parameter causes error - property not found in pscustomobject

后端 未结 3 1965
梦如初夏
梦如初夏 2020-11-28 17:08

I\'m trying to create a new Active Directory user, but first I verify that the user doesn\'t exist already with Get-ADUser. I import the user data from our HR d

3条回答
  •  南方客
    南方客 (楼主)
    2020-11-28 17:33

    The BNF for filter query strings does not allow expressions as the second operand in a comparison, only values (emphasis mine):

    Syntax:
    The following syntax uses Backus-Naur form to show how to use the PowerShell Expression Language for this parameter.

    ::= "{" "}"
    ::= | |
    ::= | "(" ")"
    ::= "-eq" | "-le" | "-ge" | "-ne" | "-lt" | "-gt"| "-approx" | "-bor" | "-band" | "-recursivematch" | "-like" | "-notlike"
    ::= "-and" | "-or"
    ::= "-not"
    ::= |
    ::= by using the specified >

    Put the value of the property you want to compare against in a variable and use that variable in the comparison. You may also want to define the filter as an actual string, if only for clarity (despite what it looks like the filter is not a scriptblock).

    $upn = $newUser.UPN
    $exists = Get-ADUser -Filter "UserPrincipalName -eq '$upn'" ...
    

提交回复
热议问题