Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Interactivity

Status
Not open for further replies.

MasterRacker

New member
Oct 13, 1999
3,343
US
I whipped up a quick and dirty script to ping a device once a minute for two hours and log the results. The core is below. It works fine for my current purposes, however one thing that surprised me was the command prompt comes back right away. I was kind of thinking I would be able to CTRL+C to break out of it. If I need to kill it, I can use taskmgr, but I was wondering if there is a better way to handle this sort of thing.
Code:
for cnt = 0 to 120
	pingResultMsg = Date & "," & Time & "," & ipAddr & "," & PingStatus(ipAddr)
	set outFile = FSO.OpenTextFile (outFQN, ForAppending, true)
	outFile.WriteLine (pingResultMsg)
	outFile.Close
	wscript.sleep(60 * 1000)  ' pause 60 seconds
next

Jeff
[small][purple]It's never too early to begin preparing for [/purple]International Talk Like a Pirate Day
"The software I buy sucks, The software I write sucks. It's time to give up and have a beer..." - Me[/small]
 
all depends what your PingStatus() Function is actually doing
 
It's markdmac's function from The only difference is I had to change the wmi query to ping something that's not a computer.
Code:
Original:
    Set objWMIService = GetObject("winmgmts:" _
      & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colPings = objWMIService.ExecQuery _
      ("SELECT * FROM Win32_PingStatus WHERE Address = '" & strComputer & "'")

Mine:
    set colPings = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery _
      ("select * from Win32_PingStatus where address = '" & strAddr & "'")

All it does is do the wmi query and return a string. The thing that surprised me is that the command window goes back to a prompt even though the for loop is still running.

My guess is that the sleep method returns control to the command session.

Jeff
[small][purple]It's never too early to begin preparing for [/purple]International Talk Like a Pirate Day
"The software I buy sucks, The software I write sucks. It's time to give up and have a beer..." - Me[/small]
 
whats your full code for the Function please? and what command windows are you talking about? i cant see anywhere in the code you have posted which will generate a dos window?
 
Here's the whole script
Code:
[small]'===============================================================================
' NAME: TemplateWithLogging.vbs
' AUTHOR: Blickblick B. Blickblick
' DATE: 05/22/2009
' DESCRIPTION: 
'     Template for a script that needs logging.
'     The information is logged to the specified file in CSV format.
'===============================================================================
dim FSO, outFQN, ipAddr, pingResultMsg, cnt

if Wscript.Arguments.Count < 2 then
	Wscript.echo ("USAGE: PingLog <ipaddr> <logfile>")
	Wscript.Quit
end if

'========================= Logfile Initialization	- Start
set FSO = WScript.CreateObject("Scripting.FileSystemObject")
ipAddr  = Wscript.Arguments(0)
outFQN  = Wscript.Arguments(1)

'----- If the output file does not exist, create it
if not FSO.FileExists(outFQN) then
   set outFile = FSO.CreateTextFile(outFQN)
end if
set outFile = nothing

'----- Open file for append
const ForAppending = 8
'========================= Logfile Initialization	- End

'----- Do stuff here
for cnt = 0 to 120
	pingResultMsg = Date & "," & Time & "," & ipAddr & "," & PingStatus(ipAddr)
	set outFile = FSO.OpenTextFile (outFQN, ForAppending, true)
	outFile.WriteLine (pingResultMsg)
	outFile.Close
	wscript.sleep(60 * 1000)  ' pause 60 seconds
next

'----- Cleanup
wscript.echo "Ping Logging Complete"
set FSO = nothing

Wscript.Quit

Function PingStatus(strAddr)
 '   On Error Resume Next
    set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery _
      ("select * from Win32_PingStatus where address = '" & strAddr & "'")
    
    For Each pingResult in objPing
        Select Case pingResult.StatusCode
            Case 0 PingStatus = "Success"
            Case 11001 PingStatus = "Status code 11001 - Buffer Too Small"
            Case 11002 PingStatus = "Status code 11002 - Destination Net Unreachable"
            Case 11003 PingStatus = "Status code 11003 - Destination Host Unreachable"
            Case 11004 PingStatus = "Status code 11004 - Destination Protocol Unreachable"
            Case 11005 PingStatus = "Status code 11005 - Destination Port Unreachable"
            Case 11006 PingStatus = "Status code 11006 - No Resources"
            Case 11007 PingStatus = "Status code 11007 - Bad Option"
            Case 11008 PingStatus = "Status code 11008 - Hardware Error"
            Case 11009 PingStatus = "Status code 11009 - Packet Too Big"
            Case 11010 PingStatus = "Status code 11010 - Request Timed Out"
            Case 11011 PingStatus = "Status code 11011 - Bad Request"
            Case 11012 PingStatus = "Status code 11012 - Bad Route"
            Case 11013 PingStatus = "Status code 11013 - TimeToLive Expired Transit"
            Case 11014 PingStatus = "Status code 11014 - TimeToLive Expired Reassembly"
            Case 11015 PingStatus = "Status code 11015 - Parameter Problem"
            Case 11016 PingStatus = "Status code 11016 - Source Quench"
            Case 11017 PingStatus = "Status code 11017 - Option Too Big"
            Case 11018 PingStatus = "Status code 11018 - Bad Destination"
            Case 11032 PingStatus = "Status code 11032 - Negotiating IPSEC"
            Case 11050 PingStatus = "Status code 11050 - General Failure"
            Case Else PingStatus = "Status code " & pingResult.StatusCode & " - Unable to determine cause of failure."
        End Select
    Next
End Function
'================================= END OF FILE =================================
[/small]

I'm not opening a command window from the script. I'm running the script from the command line.

Jeff
[small][purple]It's never too early to begin preparing for [/purple]International Talk Like a Pirate Day
"The software I buy sucks, The software I write sucks. It's time to give up and have a beer..." - Me[/small]
 
yeap, do a guitarzan says and start it with cscript.exe from a cmd session which is already established. would be guessing that you have a bug, remove all of the on error resume nexts so that you can see the error. add some wscript.echo's in your code so that you can see how the script is progressing on the screen and see the results inside your function, this should clearly show you if / where you have an issue
 
There are no error resumes.

I may have been a little unclear. The script runs perfectly. It ran for 2 hours, correctly logging status every minute.

The only thing I was surprised by was that after typing in the command and hitting enter, the command prompt came back after a few seconds when I expected it to sit there for 2 hours. The script continued to run in the background and function correctly.

As I said earlier, I'm suspecting that the wscript.sleep method spawns a thread or something allowing the commmand session to return to the prompt.

Jeff
[small][purple]It's never too early to begin preparing for [/purple]International Talk Like a Pirate Day
"The software I buy sucks, The software I write sucks. It's time to give up and have a beer..." - Me[/small]
 
sorry, totally the wrong end of the stick from my side. does "start /wait /cscript.exe ...getg.dgg.," make any difference?
 
I guess I'm confused about what you think is unusual... This seems normal behavior when using WScript. In fact, comment out the sleep command and extend the loop, and it does exactly the same behavior; returns to a command prompt almost immediately, and when the script is done (10 or 20 seconds later, let's say), you get your "Ping Logging Complete" msgbox.

The behavior would work as you expect if you used cscript.exe
 
If that's normal behavior then my question is answered. I'd never noticed it before, but then I've never run a 2 hour script before either.

Jeff
[small][purple]It's never too early to begin preparing for [/purple]International Talk Like a Pirate Day
"The software I buy sucks, The software I write sucks. It's time to give up and have a beer..." - Me[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top