【发布时间】:2017-07-29 16:03:50
【问题描述】:
我正在尝试解析 Windows 事件日志以列出设备上已卸载的所有软件以及由谁卸载。
这是我到现在为止的想法:
- 匹配事件 1040(应用程序卸载):
PowerShell -ExecutionPolicy ByPass -Command "Get-WinEvent -FilterHashTable @{logname=’application’; id=1040; StartTime=(get-date).AddDays(-1)} | select timecreated, level, id, message, ProviderName, User | Export-Csv -Append C:\BCM\eventerr.csv -notype"
- 获取事件中给出的“用户”:
Get-WinEvent -MaxEvents 10 | foreach {
$sid = $_.userid;
if($sid -eq $null) { return; }
$objSID = New-Object System.Security.Principal.SecurityIdentifier($sid);
$objUser = $objSID.Translate([System.Security.Principal.NTAccount]);
Write-Host $objUser.Value;
}
但它首先输出一个错误:
错误:试图执行未经授权的操作。在第 1 行 char:1 + Get-WinEvent -MaxEvents 10 | foreach { + ~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Get-WinEvent],异常 + FullyQualifiedErrorId : LogInfoUnavailable,Microsoft.PowerShell.Commands.GetWinEvent命令
然后它会输出一个包含 2 个用户的列表...
编辑:以下是没用的,因为我意识到第二个命令行不(总是?)输出正确的结果...
我尝试像这样组合这些:
PowerShell -ExecutionPolicy ByPass -Command "Get-WinEvent -MaxEvents 10 -FilterHashTable @{logname=’application’; id=1040; StartTime=(get-date).AddDays(-1)} | select timecreated, level, id, message, ProviderName, User | foreach {$sid = $_.userid; if($sid -eq $null) { return; } $objSID = New-Object System.Security.Principal.SecurityIdentifier($sid); $objUser = $objSID.Translate([System.Security.Principal.NTAccount]); Write-Host $objUser.Value;}| Export-Csv -Append C:\BCM\eventerr.csv -notype"
但我在 powershell 窗口中收到此错误:
在 line:1 char:325 + ... rityIdentifier();广告\用户 = S-1-5-21-935981524-3360503449-101602611-2988 ... + ~ 在 '(' 之后应该有一个表达式。 + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : ExpectedExpression
有人可以帮我解决这个问题吗?
提前致谢:)
【问题讨论】:
-
错在
if($sid -eq $null) { return; }之后缺少一个分号, -
您是否删除了您的评论 sodawillow?我不太习惯powershell,为什么在文件中调试会更容易?
-
实际上我意识到第二个命令行只是部分工作: Get-WinEvent : 无法检索有关安全日志的信息。错误:试图执行未经授权的操作。在 line:1 char:1 + Get-WinEvent -MaxEvents 10 | foreach { + ~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Get-WinEvent], Exception + FullyQualifiedErrorId : LogInfoUnavailable,Microsoft .PowerShell.Commands.GetWinEventCommand 然后输出2个用户的列表...
-
我编辑了原始帖子(大约 10 次...>
-
我尝试添加分号 BenH,但没有更多成功...
标签: windows powershell events logging uninstallation