本篇将介绍如何使用Powershell来测试域验证以及本地账户验证。
如果你想运行一段代码来实现域的验证,你可以通过Powershell来实现。
其运行的原理就是通过Get-Credential得到用户输入的账号和密码,然后通过获取的账号和密码来做验证。
此脚本还会应用到.net的Libarary System.DirectoryServices.DirectoryEntry
$dCred = Get-Credential
$dUsername = $dCred.username
$dPassword = $dCred.GetNetworkCredential().password
$currentDomain = "LDAP://" + ([ADSI]"").distinguishedName
$auth = New-Object System.DirectoryServices.DirectoryEntry($CurrentDomain,$dUserName,$dPassword)
if ($auth.name -eq $null)
{
"Authentication failed - please verify your username and password."
}
else
{
"Successfully authenticated with domain -> " + $($auth.Name)
}
然后本地验证同样需要通过Get-Credential 来获取用户名和密码,同时还会借用到.net Libarary中的System.DirectoryServices.AccountManagement.PrincipalContext,ValidateCredentials方法
$lCred = Get-Credential
$lUsername = $lCred.username
$lPassword = $lCred.GetNetworkCredential().password
$computer = $env:COMPUTERNAME
Add-Type -AssemblyName System.DirectoryServices.AccountManagement
$lAuth = New-Object System.DirectoryServices.AccountManagement.PrincipalContext('machine', $computer)
$lAuthcheck = $lAuth.ValidateCredentials($lUsername, $lPassword)
if ($lAuthcheck){
"Local Authentication passed."
}else{
"Local Authentication failed"
}