批量修改AD账号密码

脚本1: 自动导出用户列表并重置密码

####
# bulk reset user password
###
$path     = Split-Path -parent $MyInvocation.MyCommand.Definition
$tmppath  = $path + "\users.csv"
$log      = $path + "\reset_pwd.v2.log"
$pass     = "new_password"
$date     = Get-Date

"Processing started (on " + $date + "): " | Out-File $log -append
"--------------------------------------------" | Out-File $log -append

# export AD Users 
# with all properties, and actived
# get-aduser -filter * -properties * | where {$_.enabled -eq $false} |export-csv $tmppath -Encoding UTF8 -NoType
Get-Aduser -filter * | Where {$_.enabled -eq $true} |Export-Csv $tmppath -Encoding UTF8 -NoType

# reset password
Write-Host "[INFO]`t Reset Password As : $pass"
Import-Csv -Path $tmppath | ForEach-Object { 
    Write-Host "[INFO]`t Reset Password For User : $($_.Name) "
    Try
    {
        # Set-ADAccountPassword $_.DistinguishedName -Reset -NewPassword (ConvertTo-SecureString -AsPlainText $pass -Force)
        # Set-ADUser -ChangePasswordAtLogon $false $_.DistinguishedName
        "[INFO]`t User $($_.Name) password reseted!" | Out-File $log -append
    }
    Catch
    {
        Write-Host "[ERROR]`t Oops, something went wrong: $($_.Exception.Message)`r`n"
        "$($_.Exception.Message)" | Out-File $log -append
    }
}

"--------------------------------------------" + "`r`n" | Out-File $log -append

你可能感兴趣的:(批量修改AD账号密码)