2011-03-02 79 views
5

如何检测用户是否使用PowerShell从Windows系统(最好是适用于win7,Vista或XP)登录或注销?检测注销并登录PowerShell

我想注册每台机器的日期和时间。

预先感谢您

+0

任何具有完整源代码的最终解决方案? – Kiquenet 2013-04-09 12:27:55

回答

7

您可以从事件日志得到这个infromation:

Get-EventLog System -Source Microsoft-Windows-Winlogon 

登录有7001的实例ID,注销有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”参数来获取,事件日志得到一个远程计算机的这些事件。

+0

就是这样!谢谢贾森。 – 2011-03-03 20:44:19

+0

你能帮我这个:http://stackoverflow.com/questions/5186693/detect-switch-user-with-powershell有什么可以帮助理解更好的get-eventlog吗?谢谢 – 2011-03-03 21:21:27

1

这样的事情已经在Windows系统日志,类型从“Winlogon”。不知道如何通过powershell从那里提取信息,但至少日志记录部分已经在那里了。