?
Solved

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

Posted on 2012-03-30
5
Medium Priority
?
1,866 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 65

Accepted Solution

by:
RobSampson earned 1600 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 400 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 400 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

752 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