• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1962
  • Last Modified:

Need some help killing 'wscript.shell' process in vbscript

Hi,

I am trying to create a vbscript that can be used as a scheduled task on windows systems to test network performance. One part of this script uses iperf.exe to perform some tcp and udp tests, which is run using the wscript.shell command.

The problem I have is that sometimes when iperf runs it hangs, causing the entire script to stop functioning. The cmd window stays open on the client machine and it requires user intervention to close the cmd window so that the rest of the script can execute.

Because iperf is running outside of the vbscript when it stalls, I can't see a way to force the process to timeout.

Can anyone suggest a way to get around this?

NB: I'm not a scripting guru, so I apologise if I offend anyone with my shoddy coding!! Any improvements are welcomed :)

Thanks,

 
Const ForAppending = 8

Set objFSO = CreateObject("Scripting.FileSystemObject")

strLog = "C:\iperf\iperflog.txt"

'If iperflog.txt does not exist, create the file.
IF NOT objFSO.FileExists(strLog) THEN
	Set objFile = objFSO.CreateTextFile(strLog)
END IF

'Set these to nothing or you get a permission error when you try to append the file. WEIRD??
Set objFile = nothing
Set objFolder = nothing


'Use FSO to Open Text file ready for inputting data

Set objTextFile = objFSO.OpentextFile("C:\iperf\iperflog.txt", ForAppending)

Set objShell = CreateObject("WScript.Shell")


'Run iperf UDP tests for jitter (server1) and output results to text file

objTextFile.WriteLine
objTextFile.WriteLine Now & " -- ------------------------------------------------------------"
objTextFile.WriteLine Now & " -- RUNNING IPERF UDP TESTS TO Server1 (iperf -c 10.0.0.1 -u)"

Set objShell = CreateObject("WScript.Shell")

Set objWshScriptExec = objShell.Exec("C:\iperf.exe -c 10.0.0.1 -u")

Set objStdOut = objWshScriptExec.StdOut

Do Until objStdOut.AtEndOfStream
    strLine = objStdOut.ReadLine
    If Len(strLine) > 2 Then
        objTextFile.WriteLine Now & " -- " & strLine
    Else
        objTextFile.WriteLine strLine
    End If
Loop

objTextFile.WriteLine
objTextFile.WriteLine Now & " -- ------------------------------------------------------------"
objTextFile.WriteLine Now & " -- RUNNING PING TO Server1 (ping 10.0.0.1 -n 10 -l 1500)"

Set objWshScriptExec = objShell.Exec("ping 10.0.0.1 -n 10 -l 1500")

Set objStdOut = objWshScriptExec.StdOut

Do Until objStdOut.AtEndOfStream
    strLine = objStdOut.ReadLine
    If Len(strLine) > 2 Then
        objTextFile.WriteLine Now & " -- " & strLine
    Else
        objTextFile.WriteLine strLine
    End If
Loop

'Run traceroute to Server1 and output to iperflog.txt

objTextFile.WriteLine
objTextFile.WriteLine Now & " -- ------------------------------------------------------------"
objTextFile.WriteLine Now & " -- RUNNING TRACEROUTE TO Server1 (tracert -h 12 10.0.0.1)"

Set objWshScriptExec = objShell.Exec("tracert -h 12 10.0.0.1")

Set objStdOut = objWshScriptExec.StdOut

Do Until objStdOut.AtEndOfStream
    strLine = objStdOut.ReadLine
    If Len(strLine) > 2 Then
        objTextFile.WriteLine Now & " -- " & strLine
    Else
        objTextFile.WriteLine strLine
    End If
Loop

'Run iperf TCP tests (Server2) and output results to text file

objTextFile.WriteLine
objTextFile.WriteLine Now & " -- ------------------------------------------------------------"
objTextFile.WriteLine Now & " -- RUNNING IPERF TCP TESTS TO Server2 (iperf -c 10.0.0.2 -d -i 1)"

Set objShell = CreateObject("WScript.Shell")

Set objWshScriptExec = objShell.Exec("C:\iperf.exe -c 10.0.0.2 -d -i 1")

Set objStdOut = objWshScriptExec.StdOut

Do Until objStdOut.AtEndOfStream
    strLine = objStdOut.ReadLine
    If Len(strLine) > 2 Then
        objTextFile.WriteLine Now & " -- " & strLine
    Else
        objTextFile.WriteLine strLine
    End If
Loop

'Send 10 x ICMP Echo requests at 1500 bytes to Server2. Output results to iperflog.txt

objTextFile.WriteLine
objTextFile.WriteLine Now & " -- ------------------------------------------------------------"
objTextFile.WriteLine Now & " -- RUNNING PING TO Server2 (ping 10.0.0.2 -n 10 -l 1500)"
objTextFile.WriteLine

Set objWshScriptExec = objShell.Exec("ping 10.0.0.2 -n 10 -l 1500")

Set objStdOut = objWshScriptExec.StdOut

Do Until objStdOut.AtEndOfStream
    strLine = objStdOut.ReadLine
    If Len(strLine) > 2 Then
        objTextFile.WriteLine Now & " -- " & strLine
    Else
        objTextFile.WriteLine strLine
    End If
Loop

'Run traceroute to Server2 and output to iperflog.txt

objTextFile.WriteLine
objTextFile.WriteLine Now & " -- ------------------------------------------------------------"
objTextFile.WriteLine Now & " -- RUNNING TRACEROUTE TO Server2 (tracert -h 12 10.0.0.2)"
objTextFile.WriteLine

Set objWshScriptExec = objShell.Exec("tracert -h 12 10.0.0.2")

Set objStdOut = objWshScriptExec.StdOut

Do Until objStdOut.AtEndOfStream
    strLine = objStdOut.ReadLine
    If Len(strLine) > 2 Then
        objTextFile.WriteLine Now & " -- " & strLine
    Else
        objTextFile.WriteLine strLine
    End If
Loop

objTextFile.Close

wscript.quit

Open in new window

0
SEWS-E
Asked:
SEWS-E
  • 2
  • 2
3 Solutions
 
RobSampsonCommented:
Hi, between these two lines:
Set objWshScriptExec = objShell.Exec("C:\iperf.exe -c 10.0.0.1 -u")

Set objStdOut = objWshScriptExec.StdOut

Open in new window


add this, and see if that helps.  It should wait for iPerf for 2 minutes, before terminating it.
intSeconds = 0
While objWshScriptExec.Status = 0
	WScript.Sleep 1000
	intSeconds = intSeconds + 1
	If intSeconds >= 120 Then objWshScriptExec.Terminate
Wend

Open in new window


Regards,

Rob.
0
 
SEWS-EAuthor Commented:
Hi Rob,

I'll give that a go on Monday when i'm back in the office and see how it goes.

Thanks,

Paul
0
 
lisfolksCommented:
You were interested in some tips regarding your coding, too? Here are a couple:

- You had a comment on line 12 about setting objFile and objFolder to Nothing in order to make those variables work (which you noted was weird ;)
I would suggest replacing those objFSO lines (7 - 19 in your example above) with the following:
      'if the file doesn't exist, create it; else append to it
      IF NOT objFSO.FileExists(strLog) THEN
	      Set objTextFile = objFSO.CreateTextFile(strLog)  'note the strLog usage instead of the full string - you defined it, so use it :)
      ELSE
         Set objTextFile = objFSO.OpenTextFile(strLog, ForAppending)

Open in new window

- Remove all but the 1st occurrence of the following.  Once you have that shell object set up, you don't need to keep recreating it.
Set objShell = CreateObject("WScript.Shell")

Open in new window

- Though your script is going to end anyway, it's good form to set your objects (everything you used Set to create) to Nothing when you're done using them (the sooner, the better). So, I would add a section at the bottom, something like this:
      Set objTextFile = Nothing
      Set objFSO = Nothing
      Set objShell = Nothing
      Set objStdOut = Nothing
      Set objWshScriptExec = Nothing

Open in new window

0
 
lisfolksCommented:
And, actually, take it one step better: replace lines 7-19 in your example above with this, instead:
'if the file doesn't exist, create it; else append to it
Set objTextFile = objFSO.OpenTextFile(strLog, forAppending, True)

Open in new window


You've got the path (strLog), the forAppending as needed, and if the file isn't present initially, the True will allow it to be created.
0
 
SEWS-EAuthor Commented:
Thanks Rob for the answer, and points awarded to lisfolks for the help with tidying up the code :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now