如何使用 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/