Powershell:上次重置本地管理员帐户的密码是什么时候?
在我的上一篇文章(在这里找到)中,我写了如何确定计算机上本地管理员帐户的帐户名。既然我们知道了帐户名,那么该帐户的密码上次更改是什么时候?
管理计算机(客户端或服务器)上的本地帐户可能很麻烦,而使审核变得更简单的一件事是找出计算机上本地用户的密码有多旧。我上一篇文章中的 Get-SWLocalAdmin 函数返回一个 UserPrincipal 类型的对象,它有很多有趣的属性,例如 LastPasswordSet,它返回一个 DateTime 对象,告诉我们上次设置密码的时间。
如果我们已经知道要查询的帐户的名称,或者如果我们想查询内置管理员帐户之外的另一个帐户,我们可以使用下面的 Get-SWLocalPasswordLastSet 函数,该函数将返回 DateTime 对象。
function Get-SWLocalPasswordLastSet {
[CmdletBinding()]
param (
[Parameter(Mandatory=$true)]
[String]$UserName,
[Parameter(Mandatory=$true)]
[String]$ComputerName
)
Try {
Add-Type -AssemblyName System.DirectoryServices.AccountManagement
$PrincipalContext = New-Object System.DirectoryServices.AccountManagement.PrincipalContext([System.DirectoryServices.AccountManagement.ContextType]::Machine, $ComputerName)
$User = [System.DirectoryServices.AccountManagement.UserPrincipal]::FindByIdentity($PrincipalContext, $UserName)
$User.LastPasswordSet
}
Catch {
Write-Warning -Message "$($_.Exception.Message)"
}
}UserPrincipal 对象还有许多其他有趣的属性,如 Enabled、BadLogonCount 和 LastLogon 以及一些值得注意的方法,如 Delete、ExpirePasswordNow、GetGroups、ChangePassword 和 SetPassword。
要了解有关 UserPrincipal 类的更多信息,请参阅 MSDN:http://msdn.microsoft.com/en-us/library/system.directoryservices.accountmanagement.userprincipal(v=vs.110).aspx
下次我们将仔细研究如何重置本地帐户的密码以及如何验证密码是否已设置(或仅验证密码是否是我们期望的密码)。