Set-ADAccountPassword更改密码+ Set-ADUser更改下次登录密码

时间:2018-10-18 12:15:21

标签: windows powershell

我开始学习Powershell,以便为工作中的同事提供各种工具(HelpDesk)。我设法获得以下重置密码:

Set-ADUser -Credential $Desk -Identity $User -ChangePasswordAtLogon $true -Confirm |
    Set-ADAccountPassword $User -Credential $Desk -Reset -NewPassword (ConvertTo-SecureString -AsPlainText $Reset -Force -Verbose) -PassThru 

问题在于该脚本将仅记录这些输入之一:

  1. 如果我以Set-ADUser开头,则密码将在下次登录时更改,但不会更改为我输入的值。

  2. 如果我以Set-ADAccountPassword开头,它将更改密码,但不会提示用户在下次登录时进行切换。

我知道我的代码非常基础,但是它正在开发中,稍后我将在知道基本功能正常工作时对其进行重组。

为您提供有关脚本其余部分的背景信息: 显示各种命令的列表(解锁用户,Ping,NSlookup,远程连接和其他),然后选择所需的选项。此后,脚本将跳至与其链接的工具,并在需要时提示用户时执行。此代码中存在的变量都是通过提示窗口生成的。

编辑:我发现了一种解决方法,方法是简单地提示脚本用户确认重置密码后是否要启用“下次登录时更改密码”。添加的代码如下所示:
$ChangePWD = Read-Host -Prompt 'Do you want to enable "Change password at next logon? Y/N'if ($ChangePWD -eq "Y"){Set-ADUser -Credential $Desk -Identity $userObject -ChangePasswordAtLogon $true -confirm }

不过,感谢您提供我应该知道的所有基本信息。

2 个答案:

答案 0 :(得分:0)

您应该以不同的顺序运行这些命令。运行Set-ADUserAccountPassword时,取消选中,“用户必须在下次登录时更改密码”框。

$userObject = Get-ADUser -Identity $User
Set-ADAccountPassword $userObject -Credential $Desk -Reset -NewPassword (ConvertTo-SecureString -AsPlainText $Reset -Force -Verbose)
Set-ADUser -Credential $Desk -Identity $userObject -ChangePasswordAtLogon $true -confirm 

答案 1 :(得分:0)

以下方法可以解决问题,并更改了广告中的漫游器条目:

EDIT: I found a way around it by simply prompting the user of my script to confirm after resetting the password if they want to enable "Change password at next logon". The added code looks like this:

$ ChangePWD =读取主机-提示'您是否要启用“在下次登录时更改密码?是/否”,如果($ ChangePWD -eq“ Y”){Set-ADUser-凭据$ Desk -Identity $ userObject -ChangePasswordAtLogon $ true-确认}