VB 使用 WMI - 获取登录用户 [英] VB using WMI - get logged in users
问题描述
我如何使用带有 WMI 的 VB 脚本来获取登录用户的数量.我的安装只能有一个用户登录,如果有多个用户登录(通过使用 Citrix 的终端服务),则需要报告错误.我对 Citrix 了解不多,但是带有 LogonType = 10
的 Win32_LogonSession
似乎返回了各种垃圾(端口会话等).我只需要用户...是否有任何 WMI 调用可以让我获得登录 Citrix 的用户数量?下面是我的 VB 代码片段:
How can I use VB scripting with WMI to get the # of logged in users. My installation can only have one user logged in and needs to report an error if more than one user is logged in (via terminal service using Citrix). I don't know that much about Citrix but the Win32_LogonSession
with LogonType = 10
seems to return all kinds of junk (ports sessions, etc.). I just need the users...are there any WMI calls that I can just get the # of users logged into Citrix? Below is a snip of my VB code:
Set objWMIService = _
GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\cimv2")
Set colComputer = _
objWMIService.ExecQuery("Select * from Win32_LogonSession Where LogonType = 10")
谢谢!-jp
推荐答案
下面的代码应该可以帮到你(使用 strComputer="."
for local computer or strComputer="MachineName"
):
The following code should help you out (use strComputer="."
for local computer or strComputer="MachineName"
):
strComputer = "."
Set objWMI = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colSessions = objWMI.ExecQuery _
("Select * from Win32_LogonSession Where LogonType = 10")
If colSessions.Count = 0 Then
Wscript.Echo "No interactive users found"
Else
WScript.Echo "RDP Sessions:"
For Each objSession in colSessions
Set colList = objWMI.ExecQuery("Associators of " _
& "{Win32_LogonSession.LogonId=" & objSession.LogonId & "} " _
& "Where AssocClass=Win32_LoggedOnUser Role=Dependent" )
For Each objItem in colList
WScript.Echo "Username: " & objItem.Name & " FullName: " & objItem.FullName
Next
Next
End If
原始代码在这里:
这确实适用于 Windows 2003,我不能保证以后的版本.
This did work with Windows 2003, I can't make any guarantees about later version.
这篇关于VB 使用 WMI - 获取登录用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!