我正在尝试编写一个 powershell 脚本,基本上可以自动执行帐户锁定工具。理想情况下,我将能够获得一个相当有效的查询,该查询可以识别最近锁定的帐户,然后从我们的 DC 中检索该数据,并可能发送一封电子邮件,让我们知道谁被锁定以及来自安全日志的“消息”副本。
这是我目前所拥有的:我读到要使用 Get-WinEvent,我们必须使用哈希表,因此我创建了一个哈希表对象并通过日期时间变量扩展到哈希表中,它们看起来是正确的,如果我运行类似 $hash.starttime | gm 的程序,我可以确认它仍然是一个 system.datetime 对象。
$LockedOut = Get-ADUser -Properties AccountLockoutTime,LastBadPasswordAttempt,BadPwdCount,LockedOut -Filter * | ?{$_.AccountLockOutTime -ge (Get-Date).AddHours(-3)}
$LockedOut | ft name,samaccountname,LockedOut,AccountLockoutTime,BadPwdCount,LastBadPasswordAttempt
$DomainControllers = Get-ADDomainController -Filter *
ForEach($lockeduser in $LockedOut)
{
$lockeduser.Name
ForEach($DC in $DomainControllers.name)
{
$before = ($lockeduser.AccountLockoutTime.AddMinutes(1)).date
$after = ($lockeduser.AccountLockoutTime.AddMinutes(-1)).date
$hash = $null
$hash = @{}
$hash.Add("Logname", "security")
$hash.Add("Starttime", $after)
$hash.Add("Endtime", $before)
$DC
$messagecriteria = $lockeduser.Name
$message = Get-WinEvent -ComputerName $DC -FilterHashtable $hash | ?{$_.Message -like "*$messagecriteria*"}
$message
}
"----------------------------------------------------------------------------------------------------------"
}
但当我运行查询时我只得到
Get-WinEvent : No events were found that match the specified selection criteria.
At line:19 char:20
+ $message = Get-WinEvent -ComputerName $DC -FilterHashtable $hash | ?{$_ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (:) [Get-WinEvent], Exception
+ FullyQualifiedErrorId : NoMatchingEventsFound,Microsoft.PowerShell.Commands.GetWinEventCommand
答案1
我似乎让它工作了。我认为一些变量属性滞留在内存中,导致查询提供错误的时间范围。
这是成品,供感兴趣的人参考,查询已收紧,仅提取 2 秒的日志进行解析,并且仅回顾过去 10 分钟。还有一个部分用于将结果写入自定义 PSobject,这对于导出为 CSV 或 HTML 很有用。
剩下唯一要做的事情就是插入 HTML 标头并将其写入电子邮件中出现的表格中。
$LockedOut = Get-ADUser -Properties AccountLockoutTime,LastBadPasswordAttempt,BadPwdCount,LockedOut -Filter * | ?{$_.AccountLockOutTime -ge (Get-Date).AddMinutes(-10)}
$LockedOut | ft name,samaccountname,LockedOut,AccountLockoutTime,BadPwdCount,LastBadPasswordAttempt
$DomainControllers = Get-ADDomainController -Filter *
$results = $null
ForEach($lockeduser in $LockedOut)
{
$lockedusername = $lockeduser.name
ForEach($DC in $DomainControllers.name)
{
$starttime = $lockeduser.AccountLockoutTime.AddSeconds(-1)
$endtime = $lockeduser.AccountLockoutTime.AddSeconds(1)
$hash = $null
$hash = @{}
$hash.Add("Logname", "security")
$hash.Add("Starttime", $starttime)
$hash.Add("Endtime", $endtime)
"$lockedusername - Locating Events between $starttime and $endtime on $DC..."
$messagecriteria = $lockeduser.Name
$message = Get-WinEvent -ComputerName $DC -FilterHashtable $hash | ?{$_.Message -like "*$messagecriteria*"}
$message |ft @{Expression={$ExecutionContext.InvokeCommand.ExpandString($lockeduser.Name)};Label="Name"}, `
@{Expression={$ExecutionContext.InvokeCommand.ExpandString($lockeduser.SamAccountName)};Label="SAMID"},machinename,TimeCreated,ID,message
$hash.Clear()
$TC = $message.timecreated
$ID = $message.id
$messagetext = $message.message
IF($message -ne $null)
{
ForEach($line in $message)
{
$obj = New-Object -TypeName PSObject
$obj | Add-Member -NotePropertyName "Name" -NotePropertyValue $LockedUser.name
$obj | Add-Member -NotePropertyName "SamID" -NotePropertyValue $LockedUser.SamAccountName
$obj | Add-Member -NotePropertyName "DC" -NotePropertyValue $DC
$obj | Add-Member -NotePropertyName "TimeCreated" -NotePropertyValue $line.TimeCreated
$obj | Add-Member -NotePropertyName "Event ID" -NotePropertyValue $line.ID
$obj | Add-Member -NotePropertyName "Message" -NotePropertyValue $line.message
[Array]$results += $obj
}
}
}
"----------------------------------------------------------------------------------------------------------"
}
"Table of results"
$results | ft