转自:http://www.pstips.net/finding-logon-failures-2.html
每当用户使用了无效的凭证,将会生成一条安全日志。
下面这个函数实例能读到这些事件从安全日志(注意需要管理员权限),然后显示出所有错误登陆信息。
# requires Admin privileges!
function Get-LogonFailure
{
param($ComputerName)
try
{
Get-EventLog -LogName security -EntryType FailureAudit -InstanceId 4625 -ErrorAction Stop @PSBoundParameters |
ForEach-Object {
$domain, $user = $_.ReplacementStrings[5,6]
$time = $_.TimeGenerated
"Logon Failure: $domain\$user at $time"
}
}
catch
{
if ($_.CategoryInfo.Category -eq 'ObjectNotFound')
{
Write-Host "No logon failures found." -ForegroundColor Green
}
else
{
Write-Warning "Error occured: $_"
}
}
}
注意这个函数也可以远程操作,使用-ComputerName参数去查询远程的电脑. 此时目标的电脑需要开启Remote Registry服务和具有它的本地管理员的权限。