当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 使用 PowerShell 在 Active Directory 中重置密码

作者:精品下载站 日期:2024-12-14 07:15:36 浏览:15 分类:玩电脑

使用 PowerShell 在 Active Directory 中重置密码


我之前写过如何生成随机密码以及如何以拼音字母输出密码。今天我将在一个实际示例中使用这两个函数。

场景:卡尔是一名服务台代理,今天他感到有点压力。其中一个关键业务系统出现故障,电话一直响个不停。其中一个电话是一个用户忘记了密码,卡尔很快询问了常见的控制问题,以确保该用户就是他所声称的人,找到用户帐户并单击重置密码。 Carl 是一位经验丰富的特工,通常非常擅长快速编出随机密码,但今天他找不到灵感,所以他快速输入了他听同事多次使用的密码,Summer15。电话线上排着长队,他根本没有时间去做正确的事情。他还忘记了检查那个强制用户在下次登录时更改密码的小复选框。这些事情偶尔会重复发生一次,突然有很多用户使用相同的密码。

使用 Active Directory 用户和计算机在 Active Directory 中重置用户密码非常耗时。使用 Active Directory 管理中心会更快一点,因为它具有“重置密码”磁贴。但这两种选择都取决于技术人员来编写密码。

在 Active Directory PowerShell 模块中使用 cmdlet 执行相同的操作需要大量输入,并不是一个好的替代方案。除非您将所有 cmdlet 组合在一个高级函数中。让我们首先看一下使用 PowerShell 在 Active Directory 中重置密码的过程:

  1. 首先,我需要创建一个新密码并将其转换为安全字符串。

    $SecPaswd= ConvertTo-SecureString -String ‘Summer2014’ -AsPlainText -Force
  2. 然后我使用 Set-ADAccountPassword 重置密码。

    Set-ADAccountPassword -Reset -NewPassword $SecPaswd -Identity john.smith
  3. 现在我可以检查帐户是否被锁定,但我通常只是解锁帐户,无论锁定与否。

    Unlock-ADAccount -Identity john.smith
  4. 最后我确保用户必须在下次登录时更改密码

    Set-ADUser -Identity john.smith -ChangePasswordAtLogon $true

需要输入大量内容,而让用户在电话中等待并手动输入密码绝不是一个好的做法。为了解决这种情况,我编写了带有四个参数的 Reset-SWADPassword 函数:

  • Identity 采用帐户名或可分辨名称,用于标识要重置密码的帐户。
  • 长度 定义新密码的数字字符,默认为 8
  • InputStrings 定义字符组的可选字符串列表。生成的密码将包含每个字符串中至少一个字符。
  • NoChange 指定用户下次登录时不必更改密码。

该函数将利用我之前发布的两个函数 New-SWRandomPassword 和 Get-Phonetic 来生成新的随机密码并使用 NATO 语音拼写输出密码,以便用户可以更轻松地通过电话读取新密码。

在安装了 Active Directory PowerShell 模块的计算机上加载这三个函数,您只需运行以下命令即可重置用户密码:

Reset-SWADPassword -Identity phil.brath

[玩转系统] 使用 PowerShell 在 Active Directory 中重置密码

这向我们表明 Phil 现在拥有密码 ixi9EG!e

默认情况下,参数 -InputStrings 具有以下值:

$InputStrings = @('abcdefghijkmnopqrstuvwxyz', 'ABCEFGHJKLMNPQRSTUVWXYZ', '23456789', '!"#%&')

这意味着每个密码都由每个字符串中的一个字符组成。如果我发现这种方式太复杂,我可以指定我自己的输入字符串。然而,每次我想重置密码时都指定输入字符串是不可能的。解决方案是在自动变量 $PSDefaultParameterValues 中设置默认参数值。 $PSDefaultParameterValues 是 DefaultParameterDictionary 类型的特殊变量,它具有类似哈希表的结构。要设置默认参数值,我对变量进行索引,如下所示:

$PSDefaultParameterValues["Reset-SWADPassword:InputStrings"]=@('abcd', 'ABCD', '1234567890')

现在我可以再次运行 Reset-SWADPassword,而无需指定我的自定义输入字符串。

[玩转系统] 使用 PowerShell 在 Active Directory 中重置密码

这次生成的密码仅包含小写 a-d、大写 A-D 和数字 0-9。

要了解有关 $PSDefaultParameterValues 的更多信息,请参阅帮助文档 about_Parameters_Default_Values

Get-Help about_Parameters_Default_Values

每次我关闭 PowerShell 窗口时,$PSDefaultParameterValues 都会重置。如果我想在每次打开 PowerShell 时设置此值,我必须将其放入我的个人资料中。配置文件是一个每次启动 PowerShell 时都会运行的脚本,我已经写了一篇关于使用 PowerShell 配置文件的文章。只需将 $PSDefaultParameterValues 的修改添加到相应的配置文件脚本中即可!

这是完整的功能:

function Reset-SWADPassword {
<#
.Synopsis
Reset the password for specified user to a random password.
.DESCRIPTION
Uses the functions New-SWRandomPassword and Get-Phonetic to generate a random
password with specified length, reset the specified user's password and display
the new password with phonetic spelling.

Will unlock specified user and unless the parameter -NoChange is specified the user
will have to change its password on next logon.
.EXAMPLE
Example of how to use this cmdlet
.EXAMPLE
Another example of how to use this cmdlet
#>
[CmdletBinding(SupportsShouldProcess=$true,ConfirmImpact='Medium')]
param(
# Identity of user that should have their pasword reset
[Parameter(Mandatory=$true)]
[Alias('DistinguishedName')]
[String]$Identity,

# Length of password that will be generated
[ValidateRange(1,[int]::MaxValue)]
[int]$Length = 8,

# Specifies an array of strings containing charactergroups from which the password will be generated.
# At least one char from each group (string) will be used.
[String[]]$InputStrings = @('abcdefghijkmnopqrstuvwxyz', 'ABCEFGHJKLMNPQRSTUVWXYZ', '23456789', '!"#%&'),

# Specifies that the user will not have to change their password on next logon
[Switch]$NoChange
)
try {
$Password = New-SWRandomPassword -PasswordLength $Length -InputStrings $InputStrings
$SecPaswd = ConvertTo-SecureString -String $Password -AsPlainText -Force
if ($PSCmdlet.ShouldProcess("`n$DistinguishedName",'Reset password')) {
$ADUser = Set-ADAccountPassword -Reset -NewPassword $SecPaswd -Identity $Identity -PassThru -Confirm:$false -WhatIf:$false -ErrorAction Stop
Write-Verbose -Message 'Password reset successfully'
if (-Not $NoChange) {
Set-ADUser -ChangePasswordAtLogon $true -Identity $ADUser -Confirm:$false -WhatIf:$false -ErrorAction Stop
Write-Verbose -Message 'Change password at logon set to True'
}
Unlock-ADAccount -Identity $ADUser -Confirm:$false -WhatIf:$false -ErrorAction Stop
Write-Verbose -Message 'Useraccount unlocked'
Get-Phonetic -Char $Password
}
}
catch {
throw
}
}

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯