Powershell Passing Variable to -Filter with Wild Card

前端 未结 1 1034
礼貌的吻别
礼貌的吻别 2021-01-26 06:25

I am having a hard time here on this one and I know its probably a simple syntax problem. I don\'t know how to pass this variable into the code chunk and have it acknowledged co

相关标签:
1条回答
  • 2021-01-26 06:56

    Strange behaviour, this is not THE answer but a turn arround ; personnaly, I use -LDAPFilter :

    Get-ADUser -LDAPFilter "(userprincipalname=$user*)"
    

    The polish notation for the filter is a bit disconcerting at the begining, but here, it's the natural way of filtering using the underlaying protocol LDAP.

    Get-ADUser -LDAPFilter "(|(userprincipalname=$user*)(samAccountName=$user))"
    

    You can get more information about this syntax in Search Filter Syntax, you can also get corresponding filters in About_ActiveDirectory_Filter.


    If you really want to use the -Filter syntax you can do the following (I'am not so proud of that):

    $userstar = "$user*"
    Get-ADUser -Filter {(userprincipalname -like $userstar) -or (samAccountName -like $user)}
    
    0 讨论(0)
提交回复
热议问题