Option Explicit
' Global Variables
Dim gsOutput, gsHeader
Call ReadWriteFile
Sub ReadWriteFile()
' Version 1.0
' Declare Variables
Dim fso, oFile
Dim arrComputers
Dim sReadResults
Dim i
' fso Constants
Const ForReading = 1 'Used for file open
Const ForWriting = 2 'Used for file open
Const ForAppending = 8 'Used for file open
' User Defined constants
Const sFileLocation = "C:\ComputerList.txt" '<_______Change to suit
Const sSaveFile = "C:\SchedTasksReport.csv" '<_______Change to suit
' Set the Header
gsHeader = "HostName,TaskName,NextRunTime,Status"
' Create Objects
Set fso = CreateObject("Scripting.FileSystemObject")
Set oFile = fso.OpenTextFile(sFileLocation, ForReading, False)
If Err.Number <> 0 Then
WScript.Echo "Error reading File " & sfileLocation
Exit Sub
End If
' Read the whole contents of the File
Do While oFile.AtEndOfStream <> True
sReadResults = oFile.ReadAll
Loop
' Close the file
oFile.Close
' Release the object from memory
Set oFile = Nothing
' Create an array of computers
arrComputers = Split(sReadResults, vbNewLine)
' Loop each computer
For i = 0 to Ubound(arrComputers)
If arrComputers(i) = "" Then
Exit For
End If
If Reachable(arrComputers(i)) = True Then
ShowProgress arrComputers(i)
Call EnumSchedTasks(arrComputers(i))
Else
ShowProgress arrComputers(i) & " Unreachable"
gsOutput = gsOutput & arrComputers(i) & ",Unreachable" & vbNewLine
End If
Next ' i
' Write or append the results
If Not fso.FileExists(sSaveFile) Then
Set oFile = fso.OpenTextFile(sSaveFile, ForWriting, True)
If Err.Number <> 0 Then
ShowProgress "Error creating file " & sSaveFile
Exit Sub
End If
ofile.Write(gsHeader & gsOutput)
Else
Set oFile = fso.OpenTextFile(sSaveFile, ForAppending, False)
If Err.Number <> 0 Then
ShowProgress "Error opening file " & sSaveFile
Exit Sub
End If
ofile.Write(gsOutput)
End If
' Close the file
oFile.Close
' Release the object from memory
Set oFile = Nothing
End Sub ' ReadWriteFile
Sub EnumSchedTasks(sArgComputer)
' Version 1.0
' Writen by Krystian Karia
' Dated 16/03/2009
' Runs the SchTasks.exe tool to query remote shceduled tasks
' on the computers passed to us and log to a global variable
' Catch errors ourselves
' On Error Resume Next
' Declare the local variables
Dim WshShell, objCmdExec
Dim strCmdTool, strCmdArgs1, strCmdArgs2
Dim strOutput, iQuit
Const Exec_Running = 0
Const Exec_Finished = 1
' Create Objects
Set WshShell = CreateObject("Wscript.Shell")
' Set the command line arguments to work with
strCmdTool = "schtasks.exe" ' Full path and filename of cmd line tool
strCmdArgs1 = " /Query /S " ' Cmd line arguments
strCmdArgs2 = " /FO CSV /NH " ' Cmd line arguments
' Run the commandline tool and get the output
Set objCmdExec = WshShell.Exec(strCmdTool & strCmdArgs1 & sArgComputer & strCmdArgs2)
iQuit = 1
Do Until (objCmdExec.Status <> Exec_Running Or iQuit = 10)
WScript.Sleep 1000 '<___ Increase or decrease if required (1000 = 1 Second)
iQuit = iQuit + 1
Loop
If iQuit = 10 Then
'Terminate if it takes too long
ShowProgress "Terminating"
objCmdExec.Terminate
End If
strOutput = strOutput & objCmdExec.StdOut.ReadAll
If (Err.Number <> 0) Or strOutput = "" Then
ShowProgress "An Error occurred. Try running manually (in Cmd) to host to see the problem"
gsOutput = gsOutput & sArgComputer & ",An Error Occured Running " & strCmdTool & vbNewLine
Err.Clear
End If
If Trim(strOutput) <> "" Then
If InStr(UCase(Replace(strOutput, vbNewLine, "")), "INFECTED SCAN") > 0 Then
ShowProgress "TaskName: " & Replace(Left(strOutput, InStr(strOutput, ",") -1), vbNewLine, "") & vbNewLine
gsOutput = gsOutput & sArgComputer & "," & Replace(strOutput, vbNewLine, "") & vbNewLine
strOutput = ""
End If
End If
End Sub ' EnumSchedTasks
Private Function Reachable(strComputer)
' Version 1.0
On Error Resume Next
Dim wmiQuery, objWMIService, objPing, objStatus
wmiQuery = "Select * From Win32_PingStatus Where " & _
"Address = '" & strComputer & "'"
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set objPing = objWMIService.ExecQuery(wmiQuery)
For Each objStatus in objPing
If IsNull(objStatus.StatusCode) Or objStatus.Statuscode <> 0 Then
Reachable = False 'if computer is unreacable, return false
Else
Reachable = True 'if computer is reachable, return True
End If
Next
End Function ' Reachable
Private Sub ShowProgress(sComment)
WScript.Echo sComment
End Sub
Option Explicit
' Global Variables
Dim gsOutput, gsHeader
Call ReadWriteFile
Sub ReadWriteFile()
' Version 1.0
' Declare Variables
Dim fso, oFile
Dim arrComputers
Dim sReadResults
Dim i
' fso Constants
Const ForReading = 1 'Used for file open
Const ForWriting = 2 'Used for file open
Const ForAppending = 8 'Used for file open
' User Defined constants
Const sFileLocation = "C:\ComputerList.txt" '<_______Change to suit
Const sSaveFile = "C:\SchedTasksReport.csv" '<_______Change to suit
' Set the Header
gsHeader = "HostName,TaskName"
' Create Objects
Set fso = CreateObject("Scripting.FileSystemObject")
Set oFile = fso.OpenTextFile(sFileLocation, ForReading, False)
If Err.Number <> 0 Then
WScript.Echo "Error reading File " & sfileLocation
Exit Sub
End If
' Read the whole contents of the File
Do While oFile.AtEndOfStream <> True
sReadResults = oFile.ReadAll
Loop
' Close the file
oFile.Close
' Release the object from memory
Set oFile = Nothing
' Create an array of computers
arrComputers = Split(sReadResults, vbNewLine)
' Loop each computer
For i = 0 to Ubound(arrComputers)
If arrComputers(i) = "" Then
Exit For
End If
If Reachable(arrComputers(i)) = True Then
ShowProgress arrComputers(i)
Call EnumATJobs(arrComputers(i))
Else
ShowProgress arrComputers(i) & " Unreachable"
gsOutput = gsOutput & arrComputers(i) & ",Unreachable" & vbNewLine
End If
Next ' i
' Write or append the results
If Not fso.FileExists(sSaveFile) Then
Set oFile = fso.OpenTextFile(sSaveFile, ForWriting, True)
If Err.Number <> 0 Then
ShowProgress "Error creating file " & sSaveFile
Exit Sub
End If
ofile.Write(gsHeader & gsOutput)
Else
Set oFile = fso.OpenTextFile(sSaveFile, ForAppending, False)
If Err.Number <> 0 Then
ShowProgress "Error opening file " & sSaveFile
Exit Sub
End If
ofile.Write(gsOutput)
End If
' Close the file
oFile.Close
' Release the object from memory
Set oFile = Nothing
End Sub ' ReadWriteFile
Sub EnumATJobs(sArgComputer)
' Version 1.0
' Writen by Krystian Karia
' Dated 16/03/2009
' Runs the SchTasks.exe tool to query remote shceduled tasks
' on the computers passed to us and log to a global variable
' Catch errors ourselves
On Error Resume Next
' Declare the local variables
Dim WshShell, objWMIService, colScheduledJobs, objJob
Dim strOutput
' Bind to WMI
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!//" & sArgComputer & "\root\cimv2")
' Enumerate teh Win32_ScheduledJob
If Err.Number = 0 Then
Set colScheduledJobs = objWMIService.ExecQuery _
("SELECT * FROM Win32_ScheduledJob")
For Each objJob in colScheduledJobs
strOutput = objJob.Caption
Next
Else
ShowProgress "Unable to bind to " & sArgComputer
gsOutput = gsOutput & sArgComputer & ",Unable to bind to" & vbNewLine
Err.Clear
End If
If Trim(strOutput) <> "" Then
If InStr(UCase(strOutput), "INFECTED SCAN") > 0 Then
ShowProgress "Caption: " & strOutput & vbNewLine
gsOutput = gsOutput & sArgComputer & "," & strOutput & vbNewLine
strOutput = ""
End If
End If
End Sub ' EnumATJobs
Private Function Reachable(strComputer)
' Version 1.0
On Error Resume Next
Dim wmiQuery, objWMIService, objPing, objStatus
wmiQuery = "Select * From Win32_PingStatus Where " & _
"Address = '" & strComputer & "'"
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set objPing = objWMIService.ExecQuery(wmiQuery)
For Each objStatus in objPing
If IsNull(objStatus.StatusCode) Or objStatus.Statuscode <> 0 Then
Reachable = False 'if computer is unreacable, return false
Else
Reachable = True 'if computer is reachable, return True
End If
Next
End Function ' Reachable
Private Sub ShowProgress(sComment)
WScript.Echo sComment
End Sub
Option Explicit
' Global Variables
Dim gsOutput, gsHeader
Call ReadWriteFile
Sub ReadWriteFile()
' Version 1.0
' Declare Variables
Dim fso, oFile
Dim arrComputers
Dim sReadResults
Dim i
' fso Constants
Const ForReading = 1 'Used for file open
Const ForWriting = 2 'Used for file open
Const ForAppending = 8 'Used for file open
' User Defined constants
Const sFileLocation = "C:\ComputerList.txt" '<_______Change to suit
Const sSaveFile = "C:\SchedTasksReport.csv" '<_______Change to suit
' Set the Header
gsHeader = "ComputerName,Task / Comment" & vbNewLine
' Create Objects
Set fso = CreateObject("Scripting.FileSystemObject")
Set oFile = fso.OpenTextFile(sFileLocation, ForReading, False)
If Err.Number <> 0 Then
WScript.Echo "Error reading File " & sfileLocation
Exit Sub
End If
' Read the whole contents of the File
Do While oFile.AtEndOfStream <> True
sReadResults = oFile.ReadAll
Loop
' Close the file
oFile.Close
' Release the object from memory
Set oFile = Nothing
' Create an array of computers
arrComputers = Split(sReadResults, vbNewLine)
' Loop each computer
For i = 0 to Ubound(arrComputers)
If arrComputers(i) = "" Then
Exit For
End If
If Reachable(arrComputers(i)) = True Then
ShowProgress arrComputers(i)
Call EnumSchedTasks(arrComputers(i))
Else
ShowProgress arrComputers(i) & " Unreachable"
gsOutput = gsOutput & arrComputers(i) & ",Offline" & vbNewLine
End If
Next ' i
' Write or append the results
If Not fso.FileExists(sSaveFile) Then
Set oFile = fso.OpenTextFile(sSaveFile, ForWriting, True)
If Err.Number <> 0 Then
ShowProgress "Error creating file " & sSaveFile
Exit Sub
End If
ofile.Write(gsHeader & gsOutput)
Else
Set oFile = fso.OpenTextFile(sSaveFile, ForAppending, False)
If Err.Number <> 0 Then
ShowProgress "Error opening file " & sSaveFile
Exit Sub
End If
ofile.Write(gsOutput)
End If
' Close the file
oFile.Close
' Release the object from memory
Set oFile = Nothing
End Sub ' ReadWriteFile
Sub EnumSchedTasks(sArgComputer)
' Version 1.0
' Writen by Krystian Karia
' Dated 16/03/2009
' Runs the SchTasks.exe tool to query remote shceduled tasks
' on the computers passed to us and log to a global variable
' Catch errors ourselves
' On Error Resume Next
' Declare the local variables
Dim WshShell, objCmdExec
Dim strCmdTool, strCmdArgs1, strCmdArgs2
Dim strOutput, iQuit
Const Exec_Running = 0
Const Exec_Finished = 1
' Create Objects
Set WshShell = CreateObject("Wscript.Shell")
' Set the command line arguments to work with
strCmdTool = "schtasks.exe" ' Full path and filename of cmd line tool
strCmdArgs1 = " /Query /S " ' Cmd line arguments
strCmdArgs2 = " /FO CSV /NH " ' Cmd line arguments
' Run the commandline tool and get the output
Set objCmdExec = WshShell.Exec(strCmdTool & strCmdArgs1 & sArgComputer & strCmdArgs2)
iQuit = 1
Do Until (objCmdExec.Status <> Exec_Running Or iQuit = 10)
WScript.Sleep 1000 '<___ Increase or decrease if required (1000 = 1 Second)
iQuit = iQuit + 1
Loop
If iQuit = 10 Then
'Terminate if it takes too long
ShowProgress "Terminating"
objCmdExec.Terminate
End If
strOutput = strOutput & objCmdExec.StdOut.ReadAll
If (Err.Number <> 0) Or strOutput = "" Then
ShowProgress "An Error occurred. Try running manually (in Cmd) to host to see the problem"
gsOutput = gsOutput & sArgComputer & "Timed Out - Check permissions" & vbNewLine
Err.Clear
End If
If Trim(strOutput) <> "" Then
If InStr(UCase(Replace(strOutput, vbNewLine, "")), "INFECTED") > 0 Then
ShowProgress "TaskName: " & Replace(Left(strOutput, InStr(strOutput, ",") -1), vbNewLine, "") & vbNewLine
gsOutput = gsOutput & sArgComputer & "," & Replace(Left(strOutput, InStr(strOutput, ",") -1), vbNewLine, "") & vbNewLine
strOutput = ""
End If
End If
End Sub ' EnumSchedTasks
Private Function Reachable(strComputer)
' Version 1.0
On Error Resume Next
Dim wmiQuery, objWMIService, objPing, objStatus
wmiQuery = "Select * From Win32_PingStatus Where " & _
"Address = '" & strComputer & "'"
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set objPing = objWMIService.ExecQuery(wmiQuery)
For Each objStatus in objPing
If IsNull(objStatus.StatusCode) Or objStatus.Statuscode <> 0 Then
Reachable = False 'if computer is unreacable, return false
Else
Reachable = True 'if computer is reachable, return True
End If
Next
End Function ' Reachable
Private Sub ShowProgress(sComment)
WScript.Echo sComment
End Sub
Option Explicit
' Global Variables
Dim gsOutput, gsHeader
' Set to True if you want no message boxes to pop
' up. Still shows errors that stops the script
Const gblnNoMsgbox = True
Call ReadWriteFile
Sub ReadWriteFile()
' Version 1.0
' Declare Variables
Dim fso, oFile
Dim arrComputers
Dim sReadResults
Dim i
' fso Constants
Const ForReading = 1 'Used for file open
Const ForWriting = 2 'Used for file open
Const ForAppending = 8 'Used for file open
' User Defined constants
Const sFileLocation = "C:\ComputerList.txt" '<_______Change to suit
Const sSaveFile = "C:\SchedTasksReport.csv" '<_______Change to suit
' Set the Header
gsHeader = "ComputerName,Task / Comment" & vbNewLine
' Create Objects
Set fso = CreateObject("Scripting.FileSystemObject")
Set oFile = fso.OpenTextFile(sFileLocation, ForReading, False)
If Err.Number <> 0 Then
WScript.Echo "Error reading File " & sfileLocation
Exit Sub
End If
' Read the whole contents of the File
Do While oFile.AtEndOfStream <> True
sReadResults = oFile.ReadAll
Loop
' Close the file
oFile.Close
' Release the object from memory
Set oFile = Nothing
' Create an array of computers
arrComputers = Split(sReadResults, vbNewLine)
' Loop each computer
For i = 0 to Ubound(arrComputers)
If arrComputers(i) = "" Then
Exit For
End If
If Reachable(arrComputers(i)) = True Then
If gblnNoMsgbox = False Then
ShowProgress arrComputers(i)
End If
Call EnumSchedTasks(arrComputers(i))
Else
If gblnNoMsgbox = False Then
ShowProgress arrComputers(i) & " Unreachable"
End If
gsOutput = gsOutput & arrComputers(i) & ",Offline" & vbNewLine
End If
Next ' i
' Write or append the results
If Not fso.FileExists(sSaveFile) Then
Set oFile = fso.OpenTextFile(sSaveFile, ForWriting, True)
If Err.Number <> 0 Then
ShowProgress "Error creating file " & sSaveFile
Exit Sub
End If
ofile.Write(gsHeader & gsOutput)
Else
Set oFile = fso.OpenTextFile(sSaveFile, ForAppending, False)
If Err.Number <> 0 Then
ShowProgress "Error opening file " & sSaveFile
Exit Sub
End If
ofile.Write(gsOutput)
End If
' Close the file
oFile.Close
' Release the object from memory
Set oFile = Nothing
End Sub ' ReadWriteFile
Sub EnumSchedTasks(sArgComputer)
' Version 1.0
' Writen by Krystian Karia
' Dated 16/03/2009
' Runs the SchTasks.exe tool to query remote shceduled tasks
' on the computers passed to us and log to a global variable
' Catch errors ourselves
' On Error Resume Next
' Declare the local variables
Dim WshShell, objCmdExec
Dim strCmdTool, strCmdArgs1, strCmdArgs2
Dim strOutput, iQuit
Const Exec_Running = 0
Const Exec_Finished = 1
' Create Objects
Set WshShell = CreateObject("Wscript.Shell")
' Set the command line arguments to work with
strCmdTool = "schtasks.exe" ' Full path and filename of cmd line tool
strCmdArgs1 = " /Query /S " ' Cmd line arguments
strCmdArgs2 = " /FO CSV /NH " ' Cmd line arguments
' Run the commandline tool and get the output
Set objCmdExec = WshShell.Exec(strCmdTool & strCmdArgs1 & sArgComputer & strCmdArgs2)
iQuit = 1
Do Until (objCmdExec.Status <> Exec_Running Or iQuit = 10)
WScript.Sleep 1000 '<___ Increase or decrease if required (1000 = 1 Second)
iQuit = iQuit + 1
Loop
If iQuit = 10 Then
'Terminate if it takes too long
If gblnNoMsgbox = False Then
ShowProgress "Terminating"
End If
objCmdExec.Terminate
End If
strOutput = strOutput & objCmdExec.StdOut.ReadAll
If (Err.Number <> 0) Or strOutput = "" Then
If gblnNoMsgbox = False Then
ShowProgress "An Error occurred. Try running manually (in Cmd) to host to see the problem"
End If
gsOutput = gsOutput & sArgComputer & "Timed Out - Check permissions" & vbNewLine
Err.Clear
End If
If Trim(strOutput) <> "" Then
If InStr(UCase(Replace(strOutput, vbNewLine, "")), "INFECTED") > 0 Then
If gblnNoMsgbox = False Then
ShowProgress "TaskName: " & Replace(Left(strOutput, InStr(strOutput, ",") -1), vbNewLine, "") & vbNewLine
End If
gsOutput = gsOutput & sArgComputer & "," & Replace(Left(strOutput, InStr(strOutput, ",") -1), vbNewLine, "") & vbNewLine
strOutput = ""
End If
End If
End Sub ' EnumSchedTasks
Private Function Reachable(strComputer)
' Version 1.0
On Error Resume Next
Dim wmiQuery, objWMIService, objPing, objStatus
wmiQuery = "Select * From Win32_PingStatus Where " & _
"Address = '" & strComputer & "'"
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set objPing = objWMIService.ExecQuery(wmiQuery)
For Each objStatus in objPing
If IsNull(objStatus.StatusCode) Or objStatus.Statuscode <> 0 Then
Reachable = False 'if computer is unreacable, return false
Else
Reachable = True 'if computer is reachable, return True
End If
Next
End Function ' Reachable
Private Sub ShowProgress(sComment)
WScript.Echo sComment
End Sub