Get-ADUser for not exact username

孤者浪人 提交于 2019-12-18 07:22:31

问题


The script below lists some user details, it works only in case I've entered the EXACT user name. Is there a method I could use to get results if I type a partial username? I mean if for example I enter "elibukin" or "eli.buk" instaed of "eli.bukin" witch is the correct username.

do {
    Write-Host "Who r we looking for ?        (type EXIT when u done)"
    $User = Read-Host
    Get-ADUser $User -Properties * |
        fl empl*,title, sam*, disp*, mail*, manager*, depa*, giv*, l, last*,
           logon*, when*
} until ($user -eq "exit")

回答1:


I would use -LDAPFilter with ambiguous name resolution (ANR).

Get-ADUser -LDAPFilter "(anr=smith)"

See https://support.microsoft.com/en-us/kb/243299 for more information about ANR.




回答2:


I have actually worked on a script much like this. I used the -like operator to accommodate partial matches. However, this might give you more than one result.

Get-ADUser -Filter ("SamAccountName -like '*$user*'")

Or use something of this format to narrow down your result:

Get-ADUser -Filter ("SamAccountName -like '*$user*' -and Name -like '*$FirstName*' -and Surname -like '*$Lastname*'")

Use -or instead of -and for a broader result.




回答3:


If you want fuzzy matching use the parameter -Filter with the -like operator:

do {
  $user = Read-Host -Prompt 'Who are we looking for (type EXIT when done)'
  if ($user -ne 'exit') {
    Get-ADUser -Filter "SamAccountName -like '*$User*'" -Properties * |
      Format-List empl*,title, sam*, disp*, mail*, manager*, depa*, giv*, l,
                  last*, logon*, when*
  }
} until ($user -eq "exit")


来源:https://stackoverflow.com/questions/34663563/get-aduser-for-not-exact-username

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!