Find All Scheduled Tasks, VBScript

Yes I do use VBScript, a lot. I was recently asked to pull a record of all the scheduled tasks on a Windows 2000 server.

The folks wanted to know as much as possible about their tasks so that they could recreate them when they migrated to Server 2003 or 08.
I found that this was no easy task. Fortunatly, others had gone before me and done the hard work. It is true, We stand on the shoulders of giants.
 
Here is my final script.
 
‘==========================================================================

‘ VBScript Source File — Created with SAPIEN Technologies PrimalScript 2007

‘ NAME: SchAgent.vbs

‘ AUTHOR: Mike Felkins , IT
‘ DATE  : 9/3/2008

‘ COMMENT: Collect Scheduled tasks from a Windows 2000 server and records them in an Excel file
‘ Target Environment
‘—————–
‘ The DLL and the scripts apply to Winxp and earlier. Both Vista and Server08 have their own DLL, version 2.

‘Requirements
‘———-
‘ You need to register TaskScheduler.dll
‘ regsvr32 TaskScheduler.dll

‘ Available at http://www.codeproject.com/KB/cs/tsnewlib.aspx
‘==========================================================================
Dim objXL
Dim strComputer
Dim objWMIService
Dim Title, Text1, Text2
Dim Message, result
‘ Define Msg Box Var
Message = "Please Enter Server Name"
Title = "Automated Scheduled Task Inventory"
Text1 = "User Input Cancelled"
Text2 = "You Entered: " & vbTab
result = InputBox(Message, Title, "Type Server Name Here", 100, 100)
‘ Evaluate the user input.
If result = "" Then
 WScript.Echo Text1
 WScript.Quit
Else
 WScript.Echo Text2 & result & "  —-> Please wait"
End If
strComputer = result
Set objXL = WScript.CreateObject("Excel.Application")
objXL.Visible = True
objXL.WorkBooks.Add
objXL.Columns(1).ColumnWidth = 20
objXL.Columns(2).ColumnWidth = 30
objXL.Columns(3).ColumnWidth = 40
objXL.Columns(4).ColumnWidth = 40
objXL.Columns(5).ColumnWidth = 40
objXL.Columns(6).ColumnWidth = 40
objXL.Cells(1, 1).Value = "Name"
objXL.Cells(1, 2).Value = "ApplicationName"
objXL.Cells(1, 3).Value = "LastRunTime"
objXL.Cells(1, 4).Value = "NextRunTime"
objXL.Cells(1, 5).Value = "Triggers"
objXL.Cells(1, 6).Value = "CommandLine"
objXL.Range("A1:F1").Select
objXL.Selection.Font.Bold = True
objXL.Selection.Interior.ColorIndex = 1
objXL.Selection.Interior.Pattern = 1 ‘xlSolid
objXL.Selection.Font.ColorIndex = 2
objXL.Columns("B:B").Select
objXL.Selection.HorizontalAlignment = &hFFFFEFDD ‘ xlLeft
Dim intIndex
intIndex = 2
Call SchAgent
objXL.ActiveWorkbook.SaveAs("C:scripts" & result & "_ScheduledJobs.xls")
If MsgBox("Finished processing. Results saved to: " & "C:scripts" &result & "_ScheduledJobs.xls" &_
            vbTab & vbcrlf & VbCrLf & "Do you want to view the results now?", _
            4 + 32, sTitle) = 6 Then
Else
 objXL.ActiveWorkbook.Close
  ‘ Quit Excel.
 objXL.Application.Quit
 
‘ Clean Up
 Set objXL = Nothing
End If 
Sub SchAgent
Set objS = CreateObject("Scheduler.SchAgent")
objS.TargetComputer = "\" & strComputer
WScript.Echo "Looking at: " & result & "  —-> Please wait"
objS.Refresh
 For Each job In objS
  objXL.Cells(intIndex, 1).Value = job.Name
  objXL.Cells(intIndex, 2).Value = job.ApplicationName
  objXL.Cells(intIndex, 3).Value = job.LastRunTime
  objXL.Cells(intIndex, 4).Value = job.NextRunTime
  
   For Each oTrig In job.Triggers
         objXL.Cells(intIndex, 5).Value = "Trigger #" & i _
         & " (Typ " & oTrig.TriggerType _
         & ") " & oTrig.Text
         i = i + 1
      Next
      
  objXL.Cells(intIndex, 6).Value = job.CommandLine
  intIndex = intIndex + 1
 Next
  
  objXL.Cells.EntireColumn.AutoFit
End Sub
 
 
 
 
 
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s