Solved

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

Posted on 2012-03-30
5
1,824 Views
Last Modified: 2012-06-27
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
Comment
Question by:SEWS-E
  • 2
  • 2
5 Comments
 
LVL 65

Accepted Solution

by:
RobSampson earned 400 total points
ID: 37790626
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
 
LVL 2

Author Comment

by:SEWS-E
ID: 37790803
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
 
LVL 9

Assisted Solution

by:lisfolks
lisfolks earned 100 total points
ID: 37792133
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
 
LVL 9

Assisted Solution

by:lisfolks
lisfolks earned 100 total points
ID: 37792188
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
 
LVL 2

Author Closing Comment

by:SEWS-E
ID: 37796294
Thanks Rob for the answer, and points awarded to lisfolks for the help with tidying up the code :)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question