确定用户登录到 Windows 的时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Determining how long the user is logged on to Windows
提问by Hershi
The need arose, in our product, to determine how long the current user has been logged on to Windows (specifically, Vista). It seems there is no straight forward API function for this and I couldn't find anything relevant with WMI (although I'm no expert with WMI, so I might have missed something).
在我们的产品中,需要确定当前用户登录 Windows(特别是 Vista)的时间。似乎没有直接的 API 函数,我找不到任何与 WMI 相关的东西(虽然我不是 WMI 的专家,所以我可能错过了一些东西)。
Any ideas?
有任何想法吗?
采纳答案by Nickolay
For people not familiar with WMI (like me), here are some links:
对于不熟悉 WMI 的人(比如我),这里有一些链接:
- MSDN page on using WMI from various languages: http://msdn.microsoft.com/en-us/library/aa393964(VS.85).aspx
- reference about Win32_Session: http://msdn.microsoft.com/en-us/library/aa394422(VS.85).aspx, but the objects in Win32_session are of type Win32_LogonSession (http://msdn.microsoft.com/en-us/library/aa394189(VS.85).aspx), which has more interesting properties.
- WMI Explorer- a tool you can use to easily run queries like the one Michal posted.
- 关于使用各种语言的 WMI 的 MSDN 页面:http: //msdn.microsoft.com/en-us/library/aa393964(VS.85).aspx
- 关于 Win32_Session 的参考:http://msdn.microsoft.com/en-us/library/aa394422(VS.85).aspx,但 Win32_session 中的对象是 Win32_LogonSession ( http://msdn.microsoft.com/en -us/library/aa394189(VS.85).aspx),它有更多有趣的特性。
- WMI Explorer- 一种可用于轻松运行查询的工具,如 Michal 发布的一个。
And here's example querying Win32_Session from VBS:
这是从 VBS 查询 Win32_Session 的示例:
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\" _
& strComputer & "\root\cimv2")
Set sessions = objWMIService.ExecQuery _
("select * from Win32_Session")
For Each objSession in sessions
Wscript.Echo objSession.StartTime
Next
It alerts 6 sessions for my personal computer, perhaps you can filter by LogonType to only list the real ("interactive") users. I couldn't see how you can select the session of the "current user".
它会为我的个人计算机提醒 6 个会话,也许您可以按 LogonType 过滤以仅列出真实(“交互式”)用户。我看不到如何选择“当前用户”的会话。
[edit] and here's a result from Google to your problem: http://forum.sysinternals.com/forum_posts.asp?TID=3755
[编辑] 这是 Google 对您的问题的结果:http: //forum.sysinternals.com/forum_posts.asp?TID=3755
回答by Gnat
In Powershell and WMI, the following one-line command will return a list of objects showing the user and the time they logged on.
在 Powershell 和 WMI 中,以下单行命令将返回显示用户及其登录时间的对象列表。
Get-WmiObject win32_networkloginprofile | ? {$_.lastlogon -ne $null} | % {[PSCustomObject]@{User=$_.caption; LastLogon=[Management.ManagementDateTimeConverter]::ToDateTime($_.lastlogon)}}
Explanation:
解释:
- Retrieve the list of logged in users from WMI
- Filter out any non-interactive users (effectively removes
NT AUTHORITY\SYSTEM
) - Reformats the user and logon time for readability
- 从 WMI 检索登录用户列表
- 过滤掉任何非交互用户(有效去除
NT AUTHORITY\SYSTEM
) - 重新格式化用户和登录时间以提高可读性
References:
参考:
- The WMI object to use: https://forum.sysinternals.com/topic3755.html
- Formatting the date/time: https://blogs.msdn.microsoft.com/powershell/2009/08/12/get-systemuptime-and-working-with-the-wmi-date-format/
回答by Micha? Piaskowski
In WMI do: "select * from Win32_Session" there you'll have "StartTime" value.
在 WMI 中执行:“select * from Win32_Session”,您将获得“StartTime”值。
Hope that helps.
希望有帮助。
回答by Matt Hanson
Using WMI, the Win32Session is a great start. As well, it should be pointed out that if you're on a network you can use Win32_NetworkLoginProfile to get all sorts of info.
使用 WMI,Win32Session 是一个很好的开始。同样,应该指出的是,如果您在网络上,则可以使用 Win32_NetworkLoginProfile 来获取各种信息。
Set logins = objWMIService.ExecQuery _
("select * from Win32_NetworkLoginProfile")
For Each objSession in logins
Wscript.Echo objSession.LastLogon
Next
Other bits of info you can collect include the user name, last logoff, as well as various profile related stuff.
您可以收集的其他信息包括用户名、上次注销以及各种与个人资料相关的内容。