SUPINFO International University

SUPINFO Institute of Information Technology
Laboratoire Microsoft




Tuer toutes les sessions de Terminal Server en un clic

Accueil > Script > terminal_server > Tuer toutes les sessions de Terminal Server en un clic

Tuer toutes les sessions de Terminal Server en un clic

Par Jean-charles DARMAGNAC,
Publiée le 18/08/2006 vers 14h.

Script VBS permettant de tuer toutes les sessions ouvertes sur Terminal Serveur.

 

 

 

' KillTsSessions.vbs
' Finds all active TS 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

 

 

 



Proposer un script

Notre avis :

Votre avis :

Réactions :
0

Votants :
1

Visites :
7575

1
2
3
4
5

Scripts Windows Suivant :

Monitorer le service Terminal Serveur



Retrouvez ci-dessous les autres sections du Laboratoire Microsoft