Solved

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

Posted on 2012-03-30
5
1,806 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks Rob for the answer, and points awarded to lisfolks for the help with tidying up the code :)
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Welcome to part one of a multi-part tutorial series, VBScript for Windows System Administrators.  The goal of this series is to teach non-programmers how to write useful VBS code to automate their environment, and perform tasks faster, and in a more…
Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now