假设我PSCrendential
在 PowerShell 中有一个使用Get-Credential
.
如何针对 Active Directory 验证输入?
现在我找到了这种方式,但我觉得它有点难看:
[void][System.Reflection.Assembly]::LoadWithPartialName("System.DirectoryServices.AccountManagement")
function Validate-Credentials([System.Management.Automation.PSCredential]$credentials)
{
$pctx = New-Object System.DirectoryServices.AccountManagement.PrincipalContext([System.DirectoryServices.AccountManagement.ContextType]::Domain, "domain")
$nc = $credentials.GetNetworkCredential()
return $pctx.ValidateCredentials($nc.UserName, $nc.Password)
}
$credentials = Get-Credential
Validate-Credentials $credentials
[编辑,两年后]对于未来的读者,请注意Test-Credential
orTest-PSCredential
是更好的名称,因为Validate
它不是有效的 powershell 动词(请参阅Get-Verb
)