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

[玩转系统] 使用 PowerShell 验证用户名和密码

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

使用 PowerShell 验证用户名和密码


介绍

尽管我们有组托管服务帐户,但各种服务和应用程序仍然使用常规用户帐户。这些帐户的密码(希望)很难记住,并且可能会被一群人共享。这意味着,当需要修改我们多年未触及的服务、计划任务或应用程序时,我真的想在开始之前确保我拥有正确的用户名和密码。我还在各种自动化场景中使用 Test-SWADCredentials 来验证我是否拥有有效的帐户或验证帐户的密码重置。我相信您可以想出更多场景,让我们开始讨论技术部分!

当然,我可以尝试挂载网络共享或使用该帐户登录某处以验证其是否有效,但服务帐户应被锁定并遵循最小权限原则,可能很难找到合适的服务、共享或位置登录以测试凭据。此外,并非所有服务都足以告诉我登录/身份验证是否因我提供了错误的密码而失败,或者帐户是否无权访问。

为了解决这种情况,我在几年前提出了我经常使用的 PowerShell 函数 Test-SWADCredential。它是 SWAD 模块的一部分,该模块是我作为概念验证而编写的模块,旨在展示如何通过为重置密码等常见任务创建简单的自定义函数来简化 Active Directory 的工作。

[玩转系统] 使用 PowerShell 验证用户名和密码

执行

此函数有两组可以使用的参数,要么采用明文形式的用户名和密码,要么采用 PowerShell PSCredential 对象。 (请注意,不建议以明文形式输入密码,除非绝对必要,否则不应使用明文形式,密码可能会以明文形式存储在计算机上的许多位置,供其他人稍后阅读。)集是使用 PowerShell 中的 ParameterSets 创建的,您可以在 PowerShell 函数和 ParameterSets 中阅读更多内容。

Test-SWADCredential 本质上只是 System.DirectoryServices.AccountManagement.PrincipalContext 上 ValidateCredentials 方法的包装,可用于验证计算机本地帐户和 Active Directory 帐户的凭据。它不需要 ActiveDirectory 模块,并且可以在 PowerShell 6.1 中运行,但只能在 Windows 上运行。

用法

在域计算机上,您只需运行:

Test-SWADCredential -Credential (Get-Credential)

这将使 PowerShell 要求输入用户名和密码,并直接对其进行验证。您可能希望在验证后将您的凭据用于某些内容,如果您将在 PowerShell 中使用它,您可以在验证它们之前将它们存储在变量中,这样您就不需要输入两次密码,也不必冒拼写错误的风险,如下所示:

$Credential = Get-Credential
Test-SWADCredential -Credential $Credential

如果您想查看您输入的密码,您可以通过将其转换为 NetworkCredential 来解密凭证中的密码,如下所示(请注意,将密码打印到屏幕上将以明文形式将其写入任何正在运行的记录日志中):

$Credential.GetNetworkCredential().Password

源代码

<#
.SYNOPSIS
Test a credential

.DESCRIPTION
Test a credential object or a username and password against a machine or domain.
Can be used to validate service account passwords.

.PARAMETER Credential
Credential to test

.PARAMETER UserName
Username to test

.PARAMETER Password
Clear text password to test.
ATT!: Be aware that the password is written to screen and memory in clear text, it might also be stored in clear text on your computer.

.PARAMETER ContextType
Set where to validate the credential.
Can be Domain, Machine or ApplicationDirectory

.PARAMETER Server
Set remote computer or domain to validate against.

.EXAMPLE
Test-SWADCredential -UserName svc-my-service -Password Kgse(70g!S.
True

.EXAMPLE
Test-SWADCredential -Credential $Cred
True
#>
function Test-SWADCredential
{
[CmdletBinding(DefaultParameterSetName='Credential')]
Param
(
[Parameter(Mandatory=$true,ParameterSetName='Credential')]
[pscredential]
$Credential,

[Parameter(Mandatory=$true,ParameterSetName='Cleartext')]
[ValidateNotNullOrEmpty()]
[string]$UserName,

[Parameter(Mandatory=$true,ParameterSetName='Cleartext')]
[string]$Password,

[Parameter(Mandatory=$false,ParameterSetName='Cleartext')]
[Parameter(Mandatory=$false,ParameterSetName='Credential')]
[ValidateSet('ApplicationDirectory','Domain','Machine')]
[string]$ContextType = 'Domain',

[Parameter(Mandatory=$false,ParameterSetName='Cleartext')]
[Parameter(Mandatory=$false,ParameterSetName='Credential')]
[String]$Server
)

try {
Add-Type -AssemblyName System.DirectoryServices.AccountManagement -ErrorAction Stop
if($PSCmdlet.ParameterSetName -eq 'ClearText') {
$EncPassword = ConvertTo-SecureString -String $Password -AsPlainText -Force
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $UserName,$EncPassword
}
try {
if($PSBoundParameters.ContainsKey('Server'))
{
$PrincipalContext = New-Object System.DirectoryServices.AccountManagement.PrincipalContext($ContextType,$Server)
}
else
{
$PrincipalContext = New-Object System.DirectoryServices.AccountManagement.PrincipalContext($ContextType)
}
}
catch {
Write-Error -Message "Failed to connect to server using contect: $ContextType"
}
try
{
$PrincipalContext.ValidateCredentials($Credential.UserName, $Credential.GetNetworkCredential().Password,'Negotiate')
}
catch [UnauthorizedAccessException]
{
Write-Warning -Message "Access denied when connecting to server."
return $false
}
catch
{
Write-Error -Exception $_.Exception -Message "Unhandled error occured"
}
}
catch {
throw
}
}

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

取消回复欢迎 发表评论:

关灯