PowerShell验证用户名和密码

验证用户名和密码通常会在内部进行定期扫描,以防止弱密码的使用。
当然,扫描时大家要注意不要超过最大错误次数,以免造成锁定

密码验证的方法这里涵盖了两种,一种是域用户,一种是本地用户

域用户的验证方法:

$Try = New-Object System.DirectoryServices.DirectoryEntry("LDAP://DC=Contoso,DC=Com", "$UserName", "$Password")

当$Try.Name 为空时,即密码错误,当$Try.Name不为空时,即表示密码验证成功,为弱口令账户


本地用户的验证方法:

Add-Type -assemblyname system.DirectoryServices.accountmanagement

$DS = New-Object System.DirectoryServices.AccountManagement.PrincipalContext([System.DirectoryServices.AccountManagement.ContextType]::Machine,"localhost")

$DS.ValidateCredentials("admin", "P@ssw0rd")

这里的验证会直接返回True或False,即是否通过验证



猜你喜欢

转载自blog.51cto.com/2902744/2122024