【问题标题】:How to get the user name of the user who uninstalled an application on windows?如何获取在windows上卸载应用程序的用户的用户名?
【发布时间】: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


【解决方案1】:

这是您的两个功能的组合:

Get-WinEvent -FilterHashTable @{logname='application'; id=1040; StartTime=(get-date).AddDays(-10)} | ? {$_.userid} | % {
    $objSID = New-Object System.Security.Principal.SecurityIdentifier ($_.userid)
    $objUser = $objSID.Translate( [System.Security.Principal.NTAccount])
    [pscustomobject]@{
        User = $objUser.Value
        timecreated = $_.timecreated
        level = $_.level
        id = $_.id
        message = $_.message
        ProviderName = $_.ProviderName
    }
} | Export-Csv -Append C:\BCM\eventerr.csv -notype

这里是一个极长的单线:

PowerShell -ExecutionPolicy ByPass -Command "Get-WinEvent -FilterHashTable @{logname='application'; id=1040; StartTime=(get-date).AddDays(-10)} | ? {$_.userid} | % {$objSID = New-Object System.Security.Principal.SecurityIdentifier ($_.userid); $objUser = $objSID.Translate( [System.Security.Principal.NTAccount]);[pscustomobject]@{User = $objUser.Value;timecreated = $_.timecreated;level = $_.level;id = $_.id;message = $_.message;ProviderName = $_.ProviderName}} | Export-Csv -Append C:\BCM\eventerr.csv -notype"

编辑:添加了Where-Object 过滤器以删除日志中没有解析A constructor was not found. Cannot find an appropriate constructor for type System.Security.Principal.SecurityIdentifier. 错误消息的用户ID 的条目。

【讨论】:

  • 谢谢,但它对我不起作用。如果我使用您的第一个命令,它会输出以下内容: New-Object : A constructor was not found。找不到类型 System.Security.Principal.SecurityIdentifier 的适当构造函数。在 line:2 char:15 + ... $objSID = New-Object System.Security.Principal.SecurityIdentifier ( ... + ~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (:) [New-Object], PSArgumentException + FullyQualifiedErrorId:CannotFindAppropriateCtor,Microsoft.PowerShell.Commands.NewObjectCommand
  • 第二个输出如下: = : 术语“=”未被识别为 cmdlet、函数、脚本文件或可运行程序的名称。检查名称的拼写,或者如果包含路径,请验证路径是否正确并重试。在 line:1 char:176 + ... ct System.Security.Principal.SecurityIdentifier (.userid); = .Transl ... + ~ + CategoryInfo : ObjectNotFound: (=:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException
  • @druid System.Security.Principal.SecurityIdentifier 自 .Net 2.0 以来一直存在。你在哪个操作系统和版本的 PowerShell 上运行它?
  • 一开始我在 Windows 10 计算机上进行了测试,但这些日志是从 Windows 7 设备中提取的……我会在 Windows 10 上再试一次,以防万一。感谢您的提示。
  • 如果我没记错的话,Windows 7 SP1 附带 .Net 3.5.1,所以我不知道你为什么会收到这个错误,除非你禁用了 .Net 框架“Windows 功能”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-19
  • 2018-08-21
  • 2012-02-17
  • 2018-06-06
  • 1970-01-01
相关资源
最近更新 更多