Chapter 04 – Scheduling Remote Programs (AT), Part IIHow to enumerate all jobs on remote computer. In Part I of this chapter, we learned the basics of scheduling native Windows AT jobs using TaskGhost.
In this section we will learn to enumerate (list) all the jobs running on a remote computer.
TaskGhost can be used to manage hundreds of AT jobs running on hundreds of computers. There’s really no limit to the number of jobs TaskGhost can manage.
If your are using TaskGhost to manage the AT jobs running on your corporate network, you will need to understand how to Schedule new jobs, Delete old jobs, Check for job errors, and Resubmit outdated or obsolete jobs.
Job enumeration is really at the heart of all of these operations.
It is however possible to store and retrieve job identifiers and the machines they are running on, but enumeration eliminates much of this type of bookkeeping.
Lets first look at the TaskGhost script for job enumeration of a single computer.
Sub main(args)
Dim server
server = “.”
'' Read Job Info – an array of jobs
Dim handle
handle = TGCtrl.NetScheduleJobEnum(server)
if handle = 0 Then
TGCtrl.Print ("No jobs running on " & server)
Exit Sub
End If
'' See how many jobs are in the array
Dim count
count = TGCtrl.NetScheduleJobEnumCount(handle)
TGCtrl.Print ("There are " & count & " jobs on " + server)
'' Enumerate all the jobs and display the interesting information
'' Remember that the index used here is NOT the JobId
Dim index
For index = 1 to count
'' Read job at 'index'
Dim hJob
hJob = TGCtrl.NetScheduleJobEnumGet(handle, index)
If hJob = 0 Then
TGCtrl.Print ("There was an error accessing index " & index)
Exit Sub
End If
'' read the job identifier
Dim JobId
JobId = TGCtrl.NetScheduleJobGetInfoJobId (hJob)
'' start a new job description
TGCtrl.Print (index & ": " & "Job " & JobId)
'' Read the job's next scheduled run time
Dim time
time = TGCtrl.NetScheduleJobGetInfoTime(hJob)
TGCtrl.Print ("next scheduled run time = " + time)
'' Read the Command Line
Dim commandline
commandline = TGCtrl.NetScheduleJobGetInfoCommand(hJob)
TGCtrl.Print ("Command line = " + commandline)
'' Read the error if there was one
Dim error
error = TGCtrl.NetScheduleJobGetInfoError(hJob)
If error = true Then
TGCtrl.Print ("Job " & JobId & " errored last time it ran")
''Else
''TGCtrl.Print ("Job " & JobId & " Ran without error")
End if
'' end job description
TGCtrl.Print ("-----------------")
TGCtrl.FreeHandle(hJob)
Next
TGCtrl.FreeHandle(handle)
End SubThis script may seem a little long, but it’s doing a lot of work so it’s ok!
The logic flow goes like this:
1. Get the number of jobs
2. For index = 1 to the number of jobs
a. Get the job X
b. Get the job id, print the job id
c. Get the job scheduled time, print the job scheduled time
d. Get the job command line, print the job command line
e. Get the job error, print the job error
f. free the handle to this job
3. Next Job
4. Free array of jobs
Now by making copies of this job and modifying it, you could:
1. Enumerate and list all jobs (current script)
2. Enumerate and delete all jobs
3. Enumerate and delete all jobs that have errors
4. Enumerate and delete all jobs that have errors and send email
5. Enumerate and list all erroneous jobs
6. Enumerate and list all jobs with a certain command line
7. Enumerate and add/delete jobs on a certain (TaskGhost) schedule
8. Etc., etc..