logging - 检测 powershell 中的注销和登录

标签 logging powershell

如何使用 powershell 检测用户是否已从 Windows 系统(最好适用于 win7、vista 或 XP)登录或注销?

我想注册每次登录和注销机器的日期和时间。

提前谢谢

最佳答案

您可以从事件日志中获取此信息:

Get-EventLog System -Source Microsoft-Windows-Winlogon

登录的 InstanceId 为 7001,注销的 InstanceId 为 7002。用户帐户是 ReplacementStrings 中的 SID。

这里有一些对您更有用的代码。

$UserProperty = @{n="User";e={(New-Object System.Security.Principal.SecurityIdentifier $_.ReplacementStrings[1]).Translate([System.Security.Principal.NTAccount])}}
$TypeProperty = @{n="Action";e={if($_.EventID -eq 7001) {"Logon"} else {"Logoff"}}}
$TimeProeprty = @{n="Time";e={$_.TimeGenerated}}
Get-EventLog System -Source Microsoft-Windows-Winlogon | select $UserProperty,$TypeProperty,$TimeProeprty

您还可以通过将“-ComputerName”参数添加到 Get-EventLog 来从远程计算机获取这些事件。

关于logging - 检测 powershell 中的注销和登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5172111/

相关文章:

windows - Powershell:$profile 指向我找不到的路径并设置永久路径

logging - TraceSource 的 Common.Logging

java - 带时间的日志文件名不能正常工作

regex - 使用 Regex/Powershell 重命名文件

powershell - Move-Item的通配符问题

loops - Powershell自定义对象-不传递foreach变量

java - 如何关闭 BayeuxServer(嵌入 Jetty)中的 DBUG 级别日志记录?

php - 使用 flume 进行分布式日志记录

Tomcat:使用 Logger 将访问日志发送到 Syslog

json - 终端进程终止,退出代码为 : 1