首次使用PowerShell登录时的新密码

时间:2012-01-16 09:45:28

标签: powershell active-directory passwords

我只是很好奇,当我使用powershell创建用户时(我也为他们自动生成psw)我如何在首次登录后设置它们应该更改密码? 以下是创建用户的代码:

    $container =[ADSI] $Connection
    $User = $container.Create("User", "cn="+$username)
$User.Put("sAMAccountName", $username)
    $User.Put("givenName", $Firstname)
    $User.Put("sn", $Surname)
    $User.Put("mail", $Email)
    $User.SetInfo()

    # Set Random Pwd and Enable Account

    $User.PsBase.Invoke("SetPassword", $Password)
    $User.PsBase.InvokeSet("AccountDisabled", $false)
    $User.SetInfo()

1 个答案:

答案 0 :(得分:3)

要在首次登录时强制更改密码,请添加:

$User.pwdLastSet = 0

因此脚本变为:

$container =[ADSI] $Connection
$User = $container.Create("User", "cn="+$username)
$User.Put("sAMAccountName", $username)
$User.Put("givenName", $Firstname)
$User.Put("sn", $Surname)
$User.Put("mail", $Email)
$User.SetInfo()

# Set Random Pwd, force password change at first logon and Enable Account

$User.PsBase.Invoke("SetPassword", $Password)
$User.pwdLastSet = 0
$User.PsBase.InvokeSet("AccountDisabled", $false)
$User.SetInfo()