在Active Directory中按真实姓名查找用户

时间:2019-01-15 09:39:54

标签: powershell active-directory

此脚本用于在AD用户丢失密码并需要重新创建密码时重置其密码。但是,假设我们不仅仅知道用户名的真实姓名,因此我们要搜索用户名并将其插入$Username

function Reset_Password_Account () {
    $Username = Read-Host "Enter your username" 
    Write-Host "Changing Password for account" $Username

    $Newpassword = Read-Host "Enter Temporary Password" -AsSecureString
    Write-Host "Running Script..."

    Set-ADAccountPassword $Username -NewPassword $Newpassword
    Write-Host "Temporary password set"

    Set-ADUser $Username -ChangePasswordAtLogon $True
    Write-Host "You can now change password on login"

    # Stop powershell from exiting after script is run
    Read-Host "Press enter to exit"
}

$Readhost = Read-Host "To run script: Enter y 
To decline script: Enter n and exit PowerShell 
Press Enter to accept your input. ( y / n )" 
switch ($ReadHost) {
    Y {Reset_Password_Account}
    N {exit}

1 个答案:

答案 0 :(得分:0)

我建议使用Ambiguous Name Resolution,它会在AD属性(链接列表)中搜索范围并找到任何匹配项。

下面的示例查询将同时返回Jimmy SmithJim Smith-Williams

Get-ADUser -LDAPFilter "(anr=Jim Smith)"

它将搜索所有命名属性以字符串"jim smith*"开头的所有对象,以及(givenName=jim*)(sn=smith*)的所有对象,以及(givenName=smith*)(sn=jim*)