[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]