删除连接本机的session

起因:公司有台打印机,由于是win2000 pro版本的,只能有10个连接,所以后来打印的人都连不上。现需要服务器自动删除一部分连接。
简单做法:隔断时间就执行net session /delete /yes即可。
我下面的方法烦琐一些,也可以判断idle time和系统时间的。

 

'codz by xeonwell
'creation date: 2007-08-20
Option Explicit
On Error Resume Next
While True
 clearSession
 Wscript.Sleep 1000 * 60 * 10
Wend

Sub clearSession()
 Dim ws,result,str,i,arr(),j
 Set ws=Wscript.CreateObject("wscript.shell")
 Set result = ws.Exec("net session")
 str = result.StdOut.ReadAll()
 Wscript.Echo "-------------------------------"
 Wscript.Echo "Time: " & Now()
' Wscript.Echo "result:" &vbcrlf & str
 str = Replace(Replace(str, Chr(13), "|||"), Chr(10), "|||")
 str = Split(str, "|||")
' Wscript.Echo UBound(str)

 j = 0
 For i = 0 To UBound(str)
  If Instr(str(i),"\\")=1 Then
   Redim Preserve arr(j)
   arr(j) = str(i)
'   arr(j) = replaceText(str(i),"\s+","|||")
'   arr(j) = Left(arr(j),Len(arr(j))-3)
   j = j + 1
  End If 
 Next 
' Wscript.Echo UBound(arr)
 If j > 0 Then 
  Wscript.Echo "There are " & (UBound(arr) + 1) & " connections"
  Wscript.Echo "Begin Clearing the sessions......"
  For i = 0 To UBound(arr)
   'execute command: net session \\computerIP /delete /yes
'   Wscript.Echo "net session " & Left(arr(i),(Instr(arr(i),"|")-1)) & " /delete /yes"
'   Set result = ws.exec("net session " & Left(arr(i),(Instr(arr(i),"|")-1)) & " /delete /yes")
   Set result = ws.exec("net session " & Left(arr(i),(Instr(arr(i)," ")-1)) & " /delete /yes")
'   Wscript.Echo result.StdOut.ReadAll()
  Next 
  Wscript.Echo "Clear Completely!"
 Else
  Wscript.Echo "There are no connections"
 End If 
 Wscript.Echo "Waiting for next job"

 Set result = Nothing 
 Set ws = Nothing
End Sub 

Function replaceText(str,pat,rep)
 If str = Null OR str = Empty Then 
  stripSpace = ""
  Exit Function 
 End If 
 Dim re
 Set re = new RegExp
 re.Global = True
 re.IgnoreCase = True
 re.Pattern = pat
 replaceText = re.Replace(str,rep)
 Set re = Nothing
End Function 

 

XeonWell Studio