Vbscript for Scheduled Tasks

Create a Scheduled Task

Schedules Notepad to run at 12:30 PM every Monday, Wednesday, and Friday.

strComputer = “.”
Set objWMIService = GetObject(“winmgmts:” _
    & “{impersonationLevel=impersonate}!” & strComputer & “rootcimv2”)

Set objNewJob = objWMIService.Get(“Win32_ScheduledJob”)

errJobCreated = objNewJob.Create _
    (“Notepad.exe”, “********123000.000000-420”, _
        True , 1 OR 4 OR 16, , , JobID)
Wscript.Echo errJobCreated

Delete All Scheduled Tasks

Deletes all the scheduled tasks on a computer. Note: WMI can only delete scheduled tasks created using the Win32_ScheduledJob class or the At.exe utility. It cannot delete tasks created using the Task Scheduler.

strComputer = “.”
Set objWMIService = GetObject(“winmgmts:” _
    & “{impersonationLevel=impersonate}!” & strComputer & “rootcimv2”)

Set colScheduledTasks = objWMIService.ExecQuery _
    (“Select * from Win32_ScheduledJob”)

For Each objTask in colScheduledTasks
    intJobID = objTask.JobID
    Set objInstance = objWMIService.Get _
        (“Win32_ScheduledJob.JobID=” & intJobID)
    objInstance.Delete
Next

Delete a Scheduled Task

Deletes the scheduled task with the Job ID of 1.

strComputer = “.”
Set objWMIService = GetObject(“winmgmts:” _
    & “{impersonationLevel=impersonate}!” & strComputer & “rootcimv2”)

Set objInstance = objWMIService.Get(“Win32_ScheduledJob.JobID=1”)

err = objInstance.Delete

List Scheduled Tasks

Enumerates all the scheduled tasks on a computer. Note: WMI can only enumerate scheduled tasks created using the Win32_ScheduledJob class or the At.exe utility. It cannot enumerate tasks created using the Task Scheduler.

strComputer = “.”
Set objWMIService = GetObject(“winmgmts:” _
    & “{impersonationLevel=impersonate}!” & strComputer & “rootcimv2”)

Set colScheduledJobs = objWMIService.ExecQuery _
    (“Select * from Win32_ScheduledJob”)

For Each objJob in colScheduledJobs
    Wscript.Echo “Caption: ” & objJob.Caption
    Wscript.Echo “Command: ” & objJob.Command
    Wscript.Echo “Days of Month: ” & objJob.DaysOfMonth
    Wscript.Echo “Days of Week: ” & objJob.DaysOfWeek
    Wscript.Echo “Description: ” & objJob.Description
    Wscript.Echo “Elapsed Time: ” & objJob.ElapsedTime
    Wscript.Echo “Install Date: ” & objJob.InstallDate
    Wscript.Echo “Interact with Desktop: ” & objJob.InteractWithDesktop
    Wscript.Echo “Job ID: ” & objJob.JobID
    Wscript.Echo “Job Status: ” & objJob.JobStatus
    Wscript.Echo “Name: ” & objJob.Name
    Wscript.Echo “Notify: ” & objJob.Notify
    Wscript.Echo “Owner: ” & objJob.Owner
    Wscript.Echo “Priority: ” & objJob.Priority
    Wscript.Echo “Run Repeatedly: ” & objJob.RunRepeatedly
    Wscript.Echo “Start Time: ” & objJob.StartTime
    Wscript.Echo “Status: ” & objJob.Status
    Wscript.Echo “Time Submitted: ” & objJob.TimeSubmitted
    Wscript.Echo “Until Time: ” & objJob.UntilTime
Next

Leave a Reply

Your email address will not be published. Required fields are marked *