dawn Master Poster

Joined: 26 Jun 2004 Posts: 311 Location: Mishawaka, IN
|
Posted: Thu Oct 30, 2008 3:05 pm Post subject: VB Script terminates all Terminal Server sessions |
|
|
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 |
|