我正在努力使用下面的代碼。我正在嘗試通過 powershell 將安全事件日志放入一個陣列中,并只選擇我感興趣的值。在下一步中,我想遍歷這個陣列并將這些值寫入人類可讀的日志中。除了將 SID 轉換為回圈中的帳戶名稱之外,一切都很好地結合在一起。
這就是我正在做的事情:
$eventID = 4732
$log = @( Get-EventLog -LogName security -InstanceId $eventID | Select-Object -Property EventID,EntryType,TimeGenerated,
@{n="AccountName";e = {$_.replacementstrings[1]}},
@{n="GroupName";e = {$_.replacementstrings[2]}},
@{n="AdminName";e = {$_.replacementstrings[6]}}
@{n="DomainName";e = {$_.replacementstrings[3]}}
)
在此之后,我想將從事件日志中獲得的 SID 轉換為實際的帳戶名。我通過遍歷所有條目并將它們發送到輸出來做到這一點。
$log | ForEach-Object {
((New-Object System.Security.Principal.SecurityIdentifier ($_.AccountName)).Translate( [System.Security.Principal.NTAccount])).Value
}
我得到的輸出是實際驗證的帳戶名稱,它告訴我代碼有效。除了它還給我以下錯誤:
New-Object : A constructor was not found. Cannot find an appropriate constructor for type System.Security.Principal.Sec
urityIdentifier.
At line:3 char:3
((New-Object System.Security.Principal.SecurityIdentifier ($_.Account ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : ObjectNotFound: (:) [New-Object], PSArgumentException
FullyQualifiedErrorId : CannotFindAppropriateCtor,Microsoft.PowerShell.Commands.NewObjectCommand
最奇怪的部分是當我在回圈之外呼叫代碼時,它可以作業:
((New-Object System.Security.Principal.SecurityIdentifier ($log.AccountName[0])).Translate( [System.Security.Principal.NTAccount])).Value
但由于我不知道陣列將有多少條目,我需要它在回圈中作業。難道我做錯了什么?
我知道可能有更多方法可以做到這一點,但我只想了解它發生了什么以及為什么會發生。感謝任何能提供幫助的人。
uj5u.com熱心網友回復:
你@()沒用,但這不會導致錯誤,Get-EventLog將結果檢索到陣列中。
你在行尾忘記了昏迷 @{n="AdminName";e = {$_.replacementstrings[6]}}
$eventID = 4732
$log = Get-EventLog -LogName security -InstanceId $eventID | Select-Object -Property EventID,EntryType,TimeGenerated,
@{n="AccountName";e = {$_.replacementstrings[1]}},
@{n="GroupName";e = {$_.replacementstrings[2]}},
@{n="AdminName";e = {$_.replacementstrings[6]}},
@{n="DomainName";e = {$_.replacementstrings[3]}}
$log | ForEach-Object {
((New-Object System.Security.Principal.SecurityIdentifier($_.AccountName)).Translate([System.Security.Principal.NTAccount])).Value
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/405497.html
標籤:
