We help IT Professionals succeed at work.

How do I close a WSCript.shell object in VBScript?

Lico_w
Lico_w used Ask the Experts™
on
I have the following code running to monitor some software:

Set oShell = CreateObject("WSCript.shell")
hidden = 0
minimised = 7
logF = 1
sCmd = "monitorSS.bat "

testPort(logF)

function testPort(logFile)
newLog = logFile + 1
'sCmd = "monitorSS.bat test abc"
'newLog = "test8910"
'Run the BAT file.
oShell.Run sCmd & newLog, 0, false 'find out what false does
oShell.quit
if newLog < 10 then _
      testPort(newLog)

end function

This calls a bat file which telnets to a port and dumps out the response to a logfile. However the connection is not closing hence the 2nd 3rd 4th etc attempts are unable to telnet on that port.

Any help appreciated.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Software Quality Lead Engineer
Top Expert 2011
Commented:
Try slipping a delay between the two to allow the execution to complete:

WScript.Sleep 5000 '5s

Chris
Set oShell = CreateObject("WSCript.shell") 
hidden = 0
minimised = 7
logF = 1
sCmd = "monitorSS.bat "

testPort(logF)

function testPort(logFile)
newLog = logFile + 1
'sCmd = "monitorSS.bat test abc"
'newLog = "test8910"
'Run the BAT file.
oShell.Run sCmd & newLog, 0, false 'find out what false does
WScript.Sleep 5000 '5s
oShell.quit
if newLog < 10 then _
      testPort(newLog)

end function

Open in new window

WScript.Shell doesn't have a quit or dispose method I'm afraid. There are some kludges such as using oShell.SendKeys "%{F4}" to send an Alt+F4 to the active application to end it. You can ensure you are sending the keystrokes to the correct app if you know the title by doing this - oShell.AppActivate "Window Title Here"

Alternatively use the Exec rather than the Run method to return an object which you can then query for the process ID. When you have the ID pass it to kill.exe. Not pretty but should work.

Set oShellExec = oShell.Exec(sCmd)
Set oProcID = oShellExec.ProcessID

Open in new window

Can we see the batch file?  I suspect that it will be best to determine why the telnet does not close and fix that first.
Commented:
change:
oShell.Run sCmd & newLog, 0, false 'find out what false does
to:
oShell.Run sCmd & newLog, 0, true 'find out what false does

delete 'oShell.quit' out of the function
and
insert 'set oShell=nothing' on the line after testPort(logF).
Or maybe I'm just plainly talking rubbish about the quit method after checking MSDN, doh. Still, the rest of it may or may not help...
Bill PrewTest your restores, not your backups...
Top Expert 2016
Commented:
Does the batch file terminate, and the telnet program actually end, or are they stuck running?

~bp
Most Valuable Expert 2012
Top Expert 2014

Commented:
Also, if you *do* use the Exec method (objShell.Exec) you can use the Terminate method if that helps.

Set objExec = objShell.Exec("monitorSS.bat")
' ......
objExec.Terminate

Rob.

Author

Commented:
RobS using your way does terminate it, however the bat doesn't seem to run properly i.e. no response from the port and no logging???

I've attached the simple bat file for those who requested below:

@ECHO OFF
echo %1
telnet -f telnet%1.log <ip address> <port>


The issue is that once the BAT file connects via telnet it isn't quitting. I cant get it to close the connection regardless. I've tried typing quit, close, exit etc. nothing seems to break the connection other than closing the DOS window. This is what I need to resolve
I don't think that it is possible to send commands to telnet after it is started in a script like this.  Are you actually connecting to a telnet server, or a different service.  If you are not connecting to a telnet server, you could use netcat quite easily.  Something like the following:
 
nc <ip address> <port> < netcatcommands.txt > nc.log

Open in new window

You can get a windows version of netcat here:

http://joncraton.org/blog/46

Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
I'm going to explore a slightly different approach, will report back if it yields anything useful...

~bp

Author

Commented:
Thanks bp.

Sterling my internet policy won't allow me to download this!

One way I was thinking was to open the telnet session minimised, then close the window via a script. Not sure how that could be done though....
Sorry about that.  The main version of netcat that is on the site I mentioned is often caught by anti-virus, but there is another version that is linked to on the site that disables the portion of the source code that causes the problems.  Here is a link directly to the other binary:

http://www.rodneybeede.com/downloads/nc111nt_rodneybeede.zip
Most Valuable Expert 2012
Top Expert 2014
Commented:

Author

Commented:
Thanks for your suggestions, but I'd rather not download additional software especially as it seems it can be done through the telnet comand set. I have just found that the telnet session can be terminated manually with a CTRL + ]. However I'm struggling to do this through the script. My basic code is below, can anyone suggest what I'm doing wrong?


Set oShell = CreateObject("WSCript.shell") 

newLog = "1"
sCmd = "monitorSS.bat "

oShell.Run sCmd & newLog
oShell.SendKeys("^]")
oShell.SendKeys("quit")

Open in new window

Author

Commented:
Never mind I sussed it out. I just needed a sleep delay in there. Thanks for all imputs I'll try to be fair when awarding points
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
Just curious, there are a few free telnet controls that you could use directly from VBS to automate the telnet connection to your server, and then process the output right in VBS.  Before I go too far down that path, would you be able to install such a control on the server that will be running the script, or is that not allowed?

~bp