'script to get witch TS user is logged in and then logoff 'Authour: Felipe Ferreira 'Date: 03/05/2007 'Version 3.0 'nothing to improve: bug fix: was ONLY GETS ACTIVE CONNECTIONS!!!!! 'could display if was an active or discon session and ask if wants to disc 'could count time from start of script to end of script and display on report 'could check if its localhost and do not disconnect ' Check for required arguments. If Wscript.Arguments.Count < 2 Then Wscript.Echo "Arguments required. For example:" & vbCrLf _ & "cscript logoffall.vbs " & vbCrLf _ & "logoffall.vbs -> By Felipe -> version 3.0" Wscript.Quit(0) End If Set GObjArgs = WScript.Arguments inputfile = GObjArgs(1) username = GObjArgs(0) 'DECLARE VARIABLES.... Dim username, servername,inputfile, Linein, strText Dim StrSession, arrSession, i, strall, arrValues, Tserver, Tcheck Dim intUser, resultid, intUser1 Set iFSO = CreateObject("Scripting.FilesyStemObject") Set objShell = WScript.CreateObject("WScript.Shell") Set objShell2 = WScript.CreateObject("WScript.Shell") 'Should Open the servers.txt file And read each servername Execute And Get the (sessionID And servername) Set ifile = iFSO.OpenTextFile(inputfile) '----------------- go inside each file and extract each line --------- Do until ifile.AtEndOfLine servername = ifile.ReadLine 'WScript.echo "Checking server: " & servername 'Set objExecObject = objShell.Exec("quser %username% /server:" & servername ) Set objExecObject = objShell.Exec("cmd /c quser " & username & " /server:" & servername ) Do While Not objExecObject.StdOut.AtEndOfStream strText = objExecObject.StdOut.ReadLine() 'WScript.Echo strText strall = "" If Instr(strText, username) > 0 Then 'get username size (used to get ID) intUser = Len(username) intUser1 = intUser + 1 WScript.Echo "User logged in: " & servername 'WScript.Echo strText ReDim arrValues(4) arrValues = Split(strText) i = 0 Do While i < UBound(arrValues) strall = strall & arrValues(i) ' WScript.Echo "arrValues(all) " & arrValues(i) i = i + 1 Loop Trim(strall) 'WScript.Echo "all string :" & strall & "." If InStr(strall, "Disc") > 0 Then 'must get ID from string: username3Disconone5/2/200712:20 'WScript.Echo "intUser1and2: " & intUser1 resultid = Mid(strall, intUser1,2) If IsNumeric(resultid) Then resultid = resultid Else resultid = Left(resultid,1) End if ' WScript.Echo "ID :" & resultid & "." Else 'must get ID from ACTIVE conn, use old method ReDim arrSession(4) 'need to filter ID arrSession = Filter(arrValues,"rdp-tcp#") StrSession = arrSession(0) resultid = Trim(StrSession) 'WScript.Echo "ACTIVE ID: " & resultid End if 'Need to do the logoff using 2 parameters: sessionID and servername! Set objExecObject = objShell.Exec("cmd /c logoff " & resultid & " /server:" & servername ) 'WScript.Echo "Logout from: " & servername & "SessionID: " & resultid Tserver = Tserver &servername intTserver = intTserver + 1 WScript.Echo "--------------------------------------" Exit Do End If loop Tcheck = Tcheck + 1 Loop 'REPORT ACTIONS TO SCREEN WScript.Echo "Checked in : " & TCheck & " servers." If Tserver = "" Then WScript.Echo username & " is not found" Else Trim(Tserver) WScript.Echo username & " logged out from " & intTserver WScript.Echo "Logout from : " & Tserver End if WScript.Quit