Manufacturing Information Solutions Forum Index Manufacturing Information Solutions
Your Place for Support and Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

VB Script terminates all Terminal Server sessions

 
Post new topic   Reply to topic    Manufacturing Information Solutions Forum Index -> Microsoft Servers
View previous topic :: View next topic  
Author Message
dawn
Master Poster


Joined: 26 Jun 2004
Posts: 311
Location: Mishawaka, IN

PostPosted: Thu Oct 30, 2008 3:05 pm    Post subject: VB Script terminates all Terminal Server sessions Reply with quote

I found this on the web, have no tested it yet, so user-be-ware!


Code:

‘ This vbscript finds all active Terminal Server sessions on server HOST defined
‘ below and terminates them. Does not terminate the
‘ current session if being run remotely.
‘ If running on local host, set HOST value to “.”

HOST = “.”

‘ Get disconnected sessions and log them off
sessions = DisconnectedSessions(HOST)

For each session in sessions
TerminateWinSession HOST, sessionId
Next

‘ Now get active sessions and log them off
sessions = ActiveSessions(HOST)

For each session in sessions
TerminateWinSession HOST, sessionId
Next

Sub TerminateWinSession(Host, sessionId)
Dim Sh, tmpHost
Set Sh = createobject(”WScript.Shell”)
if trim(Host)=”" Then
tmpHost = “”
Else
tmpHost = ” /SERVER:” & Host
End If
Sh.Run “%COMSPEC% /C rwinsta ” & sessionId & tmpHost, 0, False
End Sub

Function ActiveSessions(Host)
Dim tmpHost, aTmp, aTmp1(), i
if trim(Host)=”" Then
tmpHost = “”
Else
tmpHost = ” /SERVER:” & Host
End If
aTmp = Split(cmd(”qwinsta” & tmpHost & ” | find “”Active”"”), _
vbCrLf)
ReDim aTmp1(-1)
For i = 0 to UBound(aTmp)
If Left(aTmp(i),1) <> “>” Then
Redim Preserve aTmp1(UBound(aTmp1) + 1)
aTmp1(UBound(aTmp1)) = Trim(Mid(aTmp(i), 42, 6))
End If
Next
ActiveSessions = aTmp1
End Function

Function DisconnectedSessions(Host)
Dim tmpHost, aTmp, aTmp1(), i
if trim(Host)=”" Then
tmpHost = “”
Else
tmpHost = ” /SERVER:” & Host
End If
aTmp = Split(cmd(”qwinsta” & tmpHost & ” | find “”Disconnected”"”), _
vbCrLf)
ReDim aTmp1(-1)
For i = 0 to UBound(aTmp)
If Left(aTmp(i),1) <> “>” Then
Redim Preserve aTmp1(UBound(aTmp1) + 1)
aTmp1(UBound(aTmp1)) = Trim(Mid(aTmp(i), 42, 6))
End If
Next
DisconnectedSessions = aTmp1
End Function

Function Cmd(cmdline)
‘ Wrapper for getting StdOut from a console command
Dim Sh, FSO, fOut, OutF, sCmd
Set Sh = createobject(”WScript.Shell”)
Set FSO = createobject(”Scripting.FileSystemObject”)
fOut = FSO.GetTempName
sCmd = “%COMSPEC% /c ” & cmdline & ” >” & fOut
Sh.Run sCmd, 0, True
If FSO.FileExists(fOut) Then
If FSO.GetFile(fOut).Size>0 Then
Set OutF = FSO.OpenTextFile(fOut)
Cmd = OutF.Readall
OutF.Close
End If
FSO.DeleteFile(fOut)
End If
End Function


_________________
Dawn Mitchell
Manufacturing Information Solutions
www.mis-group.com
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    Manufacturing Information Solutions Forum Index -> Microsoft Servers All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group