Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何获取在windows上卸载应用程序的用户的用户名?_Windows_Powershell_Events_Logging_Uninstallation - Fatal编程技术网

如何获取在windows上卸载应用程序的用户的用户名?

如何获取在windows上卸载应用程序的用户的用户名?,windows,powershell,events,logging,uninstallation,Windows,Powershell,Events,Logging,Uninstallation,我正在尝试解析windows事件日志,以列出设备上和由谁卸载的所有软件 以下是我到目前为止的想法: 匹配事件1040(应用卸载): 获取事件中给定的“用户”: 但它首先输出一个错误: 错误:试图执行未经授权的操作。。第1行 char:1+获取WinEvent-MaxEvents 10 | foreach{+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~类别信息:未指定:(:) [Get WinEvent],异常+FullyQualifiedErrorId: 登录信息不可

我正在尝试解析windows事件日志,以列出设备上和由谁卸载的所有软件

以下是我到目前为止的想法:

  • 匹配事件1040(应用卸载):

  • 获取事件中给定的“用户”:

但它首先输出一个错误:

错误:试图执行未经授权的操作。。第1行 char:1+获取WinEvent-MaxEvents 10 | foreach{+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~类别信息:未指定:(:) [Get WinEvent],异常+FullyQualifiedErrorId: 登录信息不可用,Microsoft.PowerShell.Commands.GetWinEvent‌​命令

然后它输出一个包含2个用户的列表

EDIT:以下内容是无用的,因为我意识到第二个命令行(总是?)不会输出正确的结果…

我试着这样组合这些:

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窗口中发现以下错误:

第1行字符:325 +…rityIdentifier();AD\user=S-1-5-21-935981524-3360503449-101602611-2988。。。 +~在“(”之后应该有一个表达式。 +CategoryInfo:ParserError:(:)[],ParentContainerErrorRecordException +FullyQualifiedErrorId:ExpectedExpression

有人能帮我修一下吗


提前感谢:)

以下是您的两个功能组合:

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
过滤器,以删除日志中没有用户ID的条目,解决了
未找到构造函数的问题。找不到类型System.Security.Principal.SecurityIdentifier的合适构造函数。
错误消息。

一个错误是
if($sid-eq$null)后缺少分号{return;}
,您是否删除了您的注释sodawillow?我不太习惯powershell,如果它在文件中,为什么调试会更容易?事实上,我意识到第二个命令行仅部分工作:Get-WinEvent:无法检索有关安全日志的信息。错误:尝试执行未经授权的操作..第1行:r:1+获取WinEvent-MaxEvents 10 | foreach{+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~类别信息:未指定:(:)[Get WinEvent],异常+FullyQualifiedErrorId:Loginfunavailable,Microsoft.PowerShell.Commands.GetWinEventCommand,然后它输出一个2个用户列表…我编辑了原始帖子(大约10次…>我尝试添加分号BenH,但没有取得更多成功…谢谢,但在我这方面它不起作用。如果我使用第一个命令,它会输出以下内容:New Object:找不到构造函数。找不到类型System.Security.Principal.SecurityIdentifier的合适构造函数。第2行char:15+…$objSID=New Object System.Security.Principal.SecurityIdentifier(…+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~类别信息:ObjectNotFound:(:)[新对象],PSArgumentException+FullyQualifiedErrorId:CannotFindAppropriateCtor,Microsoft.PowerShell.Commands.NewObjectCommands,其中第二个输出:=:术语“=”未被识别为cmdlet、函数、脚本文件或可操作程序的名称。请检查名称的拼写,或者如果包含路径,请验证路径是否正确更正并重试。在第1行,字符: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 framework的“windows功能”
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"