匹配来自 AD 和事件查看器的 SID
Matching SID from AD and Event Viewer
我正在尝试制作一个脚本,用于在 AD 中搜索锁定的帐户,并在事件查看器中解析安全日志,然后比较 SID,如果它们匹配,则显示具有 SID 的用户的信息.
Import-Module ActiveDirectory
$PDC = "DOMAINCONTROLLER"
$UserInfo = Search-ADAccount -LockedOut
$LockedOutEvents = Get-WinEvent -ComputerName $PDC -FilterHashtable
@{LogName='Security';Id=4740} | Sort-Object -Property * -Descending
Foreach($Event in $LockedOutEvents){
If($Event.Properties[2] -Match $UserInfo.SID.value)
{
$Event | Select-Object -Property @(
@{Label = 'User'; Expression = {$_.Properties[0].Value}}
@{Label = 'DomainController'; Expression = {$_.MachineName}}
@{Label = 'EventId'; Expression = {$_.Id}}
@{Label = 'LockoutTimeStamp'; Expression = {$_.TimeCreated}}
@{Label = 'Message'; Expression = {$_.Message -split "`r" | Select -First 1}}
@{Label = 'LockoutSource'; Expression = {$_.Properties[1].Value}}
)
}}
If 语句中的参数似乎有问题 If($Event.Properties[2] -Match $UserInfo.TargetSID)
$Event.Properties[2] 的输出是这样的:
Value
-----
S-1-1-1-111111111-111111111-111111111-22222
$UserInfo.SID.Value的输出:
S-1-1-1-111111111-111111111-111111111-11111
S-1-1-1-111111111-111111111-111111111-11111
S-1-1-1-111111111-111111111-111111111-22222
S-1-1-1-111111111-111111111-111111111-11111
S-1-1-1-111111111-111111111-111111111-11111
如您所见,在两个输出中都找到了一个 SID,但是当匹配这两个时,我得到 "False" 作为响应。有谁知道为什么会这样?
感谢您的宝贵时间。
看起来您正在将 SecurityIdentifier
对象与字符串数组进行比较(至少输出看起来像是一个数组 - 您可以使用 $UserInfo.SID.value.GetType()
来确定)。您当前的代码有两个问题:
-Match
运算符仅适用于两个字符串,因此您不能在此处使用它。但是您可以在数组上使用 Contains()
。
- 您需要将
SecurityIdentifier
转换为字符串。 Value
属性 就是这样做的。
试试这个:
If ($UserInfo.SID.value.Contains($Event.Properties[2].Value))
我正在尝试制作一个脚本,用于在 AD 中搜索锁定的帐户,并在事件查看器中解析安全日志,然后比较 SID,如果它们匹配,则显示具有 SID 的用户的信息.
Import-Module ActiveDirectory
$PDC = "DOMAINCONTROLLER"
$UserInfo = Search-ADAccount -LockedOut
$LockedOutEvents = Get-WinEvent -ComputerName $PDC -FilterHashtable
@{LogName='Security';Id=4740} | Sort-Object -Property * -Descending
Foreach($Event in $LockedOutEvents){
If($Event.Properties[2] -Match $UserInfo.SID.value)
{
$Event | Select-Object -Property @(
@{Label = 'User'; Expression = {$_.Properties[0].Value}}
@{Label = 'DomainController'; Expression = {$_.MachineName}}
@{Label = 'EventId'; Expression = {$_.Id}}
@{Label = 'LockoutTimeStamp'; Expression = {$_.TimeCreated}}
@{Label = 'Message'; Expression = {$_.Message -split "`r" | Select -First 1}}
@{Label = 'LockoutSource'; Expression = {$_.Properties[1].Value}}
)
}}
If 语句中的参数似乎有问题 If($Event.Properties[2] -Match $UserInfo.TargetSID)
$Event.Properties[2] 的输出是这样的:
Value
-----
S-1-1-1-111111111-111111111-111111111-22222
$UserInfo.SID.Value的输出:
S-1-1-1-111111111-111111111-111111111-11111
S-1-1-1-111111111-111111111-111111111-11111
S-1-1-1-111111111-111111111-111111111-22222
S-1-1-1-111111111-111111111-111111111-11111
S-1-1-1-111111111-111111111-111111111-11111
如您所见,在两个输出中都找到了一个 SID,但是当匹配这两个时,我得到 "False" 作为响应。有谁知道为什么会这样?
感谢您的宝贵时间。
看起来您正在将 SecurityIdentifier
对象与字符串数组进行比较(至少输出看起来像是一个数组 - 您可以使用 $UserInfo.SID.value.GetType()
来确定)。您当前的代码有两个问题:
-Match
运算符仅适用于两个字符串,因此您不能在此处使用它。但是您可以在数组上使用Contains()
。- 您需要将
SecurityIdentifier
转换为字符串。Value
属性 就是这样做的。
试试这个:
If ($UserInfo.SID.value.Contains($Event.Properties[2].Value))