Determine logontype with WSH VB

chadmanvb
chadmanvb used Ask the Experts™
on
I need to determine how a user is logged into a remote computer, either locally or via Remote desktop.  I have the code below and this works fine for finding the user logged on, but I can't get it to work to determine how they logged in.  I get an error about "objItem.LogonId" not being a valid property or method.  Chad
Dim strComputer , bDoThings
Dim objWMIService , colSessions , objSession , colList , objItem
strComputer = InputBox("Please Enter Server To Check for Logged In Users")
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colSessions = objWMIService.ExecQuery ("Select * from Win32_LogonSession Where LogonType = 2 OR LogonType = 10")
On Error Resume Next 
bDoThings = False 
if colSessions.Count > 0 Then 
  bDoThings =True
Else
  MsgBox ("No Sessions Exist, either no users or Win32_LogonSession does not exist")
End If
On Error Goto 0 
If bDoThings = True Then 
  For Each objSession in colSessions 
  Set colList = objWMIService.ExecQuery("Associators of "& "{Win32_LogonSession.LogonId=" & objSession.LogonId & "} " & "Where AssocClass=Win32_LoggedOnUser Role=Dependent" ) 
       For Each objItem in colList 
          WScript.Echo "Username: " & objItem.Name
          'WScript.Echo "LogonID: " & objItem.LogonId
       Next
  Next
  Wscript.Echo "Script is Complete"
End If

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_LogonSession")
Set oShell = CreateObject( "WScript.Shell" )
user=oShell.ExpandEnvironmentStrings("%UserName%")
For Each objItem in colItems
Wscript.Echo "Logon ID: " & objItem.LogonId
WScript.Echo "Username: " & user
Wscript.Echo "Logon Type: " & objItem.LogonType

Next

Author

Commented:
This kind of works, but when I check a remote computer it keeps telling me that I am the current user.  How can I get this to show the accual user logged on?  Chad
Use below instead of environment strings.

 Wscript.Echo "UserName: " & objItem.UserName & " is logged in at computer " & strComputer
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Author

Commented:
I get this error when I try that:
C:\Temp\practicevb\remotetest.vbs(17, 7) Microsoft VBScript runtime error: Object doesn't support this property or metho
d: 'objItem.UserName'
Try this

strComputer = "rplmwm06679"
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems2 = objWMIService.ExecQuery("Select * from Win32_LogonSession")
Set colItems = objWMIService.ExecQuery("Select * From Win32_ComputerSystem")
 
Set oShell = CreateObject( "WScript.Shell" )
user=oShell.ExpandEnvironmentStrings("%UserName%")
 
For Each objItem in colItems
Wscript.Echo "UserName: " & objItem.UserName & " is logged in at computer " & strComputer
Next
 
For Each objItem in colItems2
Wscript.Echo "Logon ID: " & objItem.LogonId
Wscript.Echo "Logon Type: " & objItem.LogonType
Next

Open in new window

change strcomputer ofcourse

Author

Commented:
Thanks, that works!  Chad

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial