检测注销并登录powershell

Art*_*lho 5 powershell logging

如何使用PowerShell检测用户是否已从Windows系统(最好是与win7,vista或XP一起使用)登录或注销?

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

先感谢您

Jas*_*her 7

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

Get-EventLog System -Source Microsoft-Windows-Winlogon
Run Code Online (Sandbox Code Playgroud)

登录的InstanceId为7001,注销为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
Run Code Online (Sandbox Code Playgroud)

您还可以通过向Get-EventLog添加"-ComputerName"参数从远程计算机获取这些事件.