VB using WMI - get logged in users

后端 未结 1 1492
陌清茗
陌清茗 2020-12-10 08:54

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 l

相关标签:
1条回答
  • 2020-12-10 09:37

    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 
    

    The original code is here:

    How to show logged on users? (Tek-Tips Forums)

    This did work with Windows 2003, I can't make any guarantees about later version.

    0 讨论(0)
提交回复
热议问题