匹配AD和事件查看器中的SID



我正在尝试制作一个脚本,在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()来确保(。您当前的代码有两个问题:

  1. -Match运算符只适用于两个字符串,因此不能在此处使用。但是您可以在阵列上使用Contains()
  2. 您需要将SecurityIdentifier转换为字符串。Value属性会执行此操作

试试这个:

If ($UserInfo.SID.value.Contains($Event.Properties[2].Value))

最新更新