Need help with a vbs script

Brazen

Diamond Member
Jul 14, 2000
4,259
0
0
I found this script for killing ALL open file sessions:
Set objConnection = GetObject("WinNT://atl-ws-01/LanmanServer")
Set colSessions = objConnection.Sessions

For Each objSession in colSessions
colSessions.Remove(objSession.Name)
Next

but i just want to kill one open file session. I tried doing this:
Set objConnection = GetObject("WinNT://CSVS03/LanmanServer")
Set colSessions = objConnection.Sessions

colSessions.Remove("ADMINISTRATOR\GISS04")
but then I get an error if the session does not exist (which sometimes it won't) and then the scheduled task hangs open.

I thought somehow I could test to see if the session existed with something like this:
Set objConnection = GetObject("WinNT://CSVS03/LanmanServer")
Set colSessions = objConnection.Sessions

if (objSession.Name("ADMINISTRATOR\GISS04")) Then
colSessions.Remove("ADMINISTRATOR\GISS04")
end if
but now I get a compile error saying "Object required: 'objSession'"

Can someone help me out?
 

bunker

Lifer
Apr 23, 2001
10,572
0
71
Put it back to your orignial code.

At the top put:

On Error Resume Next

Then you shouldn't get an error when that session doesn't exist, the code will continue on to completion.
Error handling in vbscript is pretty non-existant so it can be a pain.
 

oog

Golden Member
Feb 14, 2002
1,721
0
0
your compile error of object required: objSession is because you're trying to use something called objSession and in your code snippet you never declare it or set it.
 

KB

Diamond Member
Nov 8, 1999
5,406
389
126
Try

Set objConnection = GetObject("WinNT://atl-ws-01/LanmanServer")
Set colSessions = objConnection.Sessions

For Each objSession in colSessions
If objSession.Name = "ADMINISTRATOR\GISS04" Then colSessions.Remove(objSession.Name)
Next
 

Brazen

Diamond Member
Jul 14, 2000
4,259
0
0
Thanks guys, bunker and KB both had good suggestions and it was a toss up but I used bunker's suggest. Works like charm.
 

oog

Golden Member
Feb 14, 2002
1,721
0
0
i didn't think that it was objSessions that was mistyped. i thought that what was missing was a declaration of a different variable named objSession that was meant to be assigned before it was used.