Link to home
Start Free TrialLog in
Avatar of hibridassassin
hibridassassinFlag for United States of America

asked on

Display only inactive logged in remote desktop users with vbscript

I have a code that logs into a file all the active users on the machine but I need it to only log the people who leave their session but don't actually log off.  In other words  I need to log the people who "X out" rather than actually logging out.
'Login Check Script
 
'VARIABLE - Output File Path
FileOut = "C:\Logged In Users.txt"
 
'VARIABLE Set Open Text File to Append
Const Appending = 8
 
'Create the output file or open it
Set objFS = CreateObject("Scripting.FileSystemObject")
If objFS.FileExists(FileOut) Then
Set objFile = objFS.OpenTextFile(FileOut, Appending, True)
Else
Set objFile = objFS.CreateTextFile(FileOut)
objFile.WriteLine "Username" &";"& "Full Name"
End If
 
'Checked logged in users
 
strComputer = "."   
 
Set objWMI = GetObject("winmgmts:" _ 
	& "{impersonationLevel=impersonate}!\\" _ 
	& strComputer & "\root\cimv2") 
 
Set colSessions = objWMI.ExecQuery _ 
	("Select * from Win32_LogonSession Where LogonType = 10") 
 
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 
	Set objComputer = CreateObject("Shell.LocalMachine")
	objFile.WriteLine objItem.Name &";"& objItem.FullName
      Next 
   Next 
WScript.Quit

Open in new window

Avatar of davewl
davewl
Flag of United Kingdom of Great Britain and Northern Ireland image

I'm still looking into this over in this thread:

https://www.experts-exchange.com/questions/24079214/Remote-Desktop-Connection-Session-Logging.html

Keep an eye there too.
Avatar of RobSampson
Hi guys.....quser may help to identify the status of users....

This code has come from here:
https://www.experts-exchange.com/questions/23877710/Modify-VB-Script-to-allow-for-terminal-service-session-and-file-input.html

Regards,

Rob.
If LCase(Right(Wscript.FullName, 11)) = "wscript.exe" Then
    strPath = Wscript.ScriptFullName
    strCommand = "%comspec% /k cscript  """ & strPath & """"
    Set objShell = CreateObject("Wscript.Shell")
    objShell.Run(strCommand), 1, True
    Wscript.Quit
End If
 
' Set these parameters as required
strComputer = "TERMINALSERVER"
strQUser = "C:\Temp\Citrix Command Line Tools\QUser.exe"
strOutputFile = "C:\Temp\Citrix Command Line Tools\Users.txt"
'---------------------------------
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
Const intForReading = 1
strCommand = "cmd /c " & objFSO.GetFile(strQUser).ShortPath & " /SERVER:" & strComputer & " > """ & strOutputFile & """"
objShell.Run strCommand, 0, True
If objFSO.GetFile(strOutputFile).Size > 0 Then
	Set objOutputFile = objFSO.OpenTextFile(strOutputFile, intForReading, False)
	arrResults = Split(objOutputFile.ReadAll, VbCrLf)
	objOutputFile.Close
	Set objOutputFile = Nothing
	
	strResults = ""
	For Each strLine In arrResults
		If strResults = "" Then
			strResults = Trim(Mid(strLine, 2, 20))
		Else
			strResults = strResults & VbCrLf & Trim(Mid(strLine, 2, 20))
		End If
		strResults = strResults & "|" & Trim(Mid(strLine, 24, 17)) & "|" & Trim(Mid(strLine, 41, 4)) & "|" & Trim(Mid(strLine, 47, 5)) & "|" & Trim(Mid(strLine, 54, 10)) & "|" & Trim(Mid(strLine, 66))
	Next
	
	strResults = Replace(strResults, VbCrLf & "|||||", "")
	
	WScript.Echo strResults
Else
	WScript.Echo "No sessions were found on " & strComputer
End If

Open in new window

Hey Rob,

Presumably that will only play if these guys are in a Citrix environment (?)

I seem to remember from the other thread that they're in good old Win2K terminal services.

I've been trying to hunt down the attribute with little luck so far.

Is there anything we could do with Win32_PerfFormattedData_LocalSessionManager_TerminalServices.InactiveSessions?

Or Win32_TerminalService_DisconnectedSessions?

Presumably the flag these look at to count up the session numbers can be found somewhere?


Hi, I'm not sure about the WMI classes yet, but last time I tried to look for them, and their linkages, I got confused!  :-)

QUser.exe can be used against a standard Terminal Server, so you may as well try it....I think it would be easier to work with.....I'll have another look through the WMI classes.

Regards,

Rob.
SOLUTION
Avatar of davewl
davewl
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hibridassassin

ASKER

modified the code a bit and got it to work exactly what we needed.
If LCase(Right(Wscript.FullName, 11)) = "wscript.exe" Then
    strPath = Wscript.ScriptFullName
    strCommand = "command.com /c query session > c:\log.txt"
    Set objShell = CreateObject("Wscript.Shell")
    objShell.Run(strCommand), 1, True
    Wscript.Quit
End If
 
' Set these parameters as required
strComputer = "."
strQUser = "query session"
strOutputFile = "C:\log.txt"
'---------------------------------
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
Const intForReading = 1
strCommand = "cmd /c " & objFSO.GetFile(strQUser).ShortPath & " /SERVER:" & strComputer & " > """ & strOutputFile & """"
objShell.Run strCommand, 0, True
If objFSO.GetFile(strOutputFile).Size > 0 Then
        Set objOutputFile = objFSO.OpenTextFile(strOutputFile, intForReading, False)
        arrResults = Split(objOutputFile.ReadAll, VbCrLf)
        objOutputFile.Close
        Set objOutputFile = Nothing
        
        strResults = ""
        For Each strLine In arrResults
                strStatus = Trim(Mid(strLine, 47, 5))
                If strStatus = "Disc" Then
                        If strResults = "" Then
                                strResults = Trim(Mid(strLine, 2, 20))
                        Else
                                strResults = strResults & VbCrLf & Trim(Mid(strLine, 2, 20))
                        End If
                        strResults = strResults & "|" & Trim(Mid(strLine, 24, 17)) & "|" & Trim(Mid(strLine, 41, 4)) & "|" & strStatus & "|" & Trim(Mid(strLine, 54, 10)) & "|" & Trim(Mid(strLine, 66))
                End If
        Next
        
        strResults = Replace(strResults, VbCrLf & "|||||", "")
        
        WScript.Echo strResults
Else
        WScript.Echo "No sessions were found on " & strComputer
End If

Open in new window

we also accomplished the same task with this code
set objShell = CreateObject("WScript.Shell")
strRun = "command.com /c query session > c:\log.txt"
objShell.Run strRun, 1, True

Open in new window

Thanks for your help.