Topic Options
#1384 - 07/16/03 07:19 PM Chapter 04 – Scheduling Remote Programs (AT)
Luke Tomasello Administrator Offline
Member

Registered: 09/17/00
Posts: 740
Loc: San Jose, CA., USA
Chapter 04 – Scheduling Remote Programs (AT), Part I

How to schedule a job on a remote computer.

One of the powerful features of TaskGhost is its ability to manage Windows AT jobs, AKA Windows Task Scheduler.
It should be noted that Windows AT jobs are not the native format of TaskGhost jobs, but instead a job format that TaskGhost supports for completeness.

Back in the beginning, there was only AT.EXE on the old NT boxes. Microsoft then added a ‘user friendly’ front end called Task Scheduler.
When scheduling jobs either by Task Scheduler, or AT.EXE, you are really creating the same jobs. Indeed, AT.EXE is just a command line interface to the much nicer Task Scheduler.

TaskGhost will allow you to create and otherwise manage these native Windows jobs.

Lets have a look at the NetScheduleJobAdd.vbs script.

Code:
Sub main(notused)
	
	Dim server
	Dim cron_spec
	Dim commandline

	server = "."
	commandline = "CMD.EXE"

	'' schedule:	
	'' minute, hour, day of month, month, day of week
	cron_spec = "0 0 _ _ *"
	
	'' logon to remote server and schedule a job
	Dim jobId
	jobId = TGCtrl.NetScheduleJobAdd(NCP_INTERACTIVE, server, cron_spec, commandline)
	
	'' See if we get back a valid job identifier.
	If jobId > 0 Then
		TGCtrl.Print ("Added job " + commandline)
	Else
		TGCtrl.Print ("Unable to add job  " + commandline)
	End If
	
End Sub
In the above script, we are attempting to create an AT job on server "." (local machine) which is comprised of the command "CMD.EXE".
Our schedule "0 0 _ _ *", means:
  • Minute=0 - top of the hour (minute zero)
  • Hour=0 - top of the day (hour zero)
  • day of month=_ - not used
  • month=_ - not used
  • day of week=* - any (we don’t care)


For a complete explanation of this schedule format, please read about TaskGhost’s extended cron syntax .

The NCP_INTERACTIVE flag specifies that the job created will be visible and can interact with the currently logged in user.

To test this for ourselves, we’ll need to change the Server variable to be the name of a server on our network or "." if we want to schedule the job to run on the local machine.

We also want to set the CommandLine variable to whatever it is you want to launch on the remote computer. In this example we’ve set it to “CMD.EXE”. Not a very exciting program, but it should work for our purposes.

Okay, let’s try our script.
Save the file and load it directly into TaskGhost, we’re not going to use Schedule.vbs to load this script.

When run, you should see TaskGhost display something like the following:
Added job CMD.EXE

Now, if we run AT.EXE at the DOS prompt of the computer on which we scheduled the job to run on, here’s what we should see:
Code:
 C:\>AT
Status ID   Day                     Time          Command Line
-------------------------------------------------------------------------------
        1   Next M T W Th F S Su    12:00 AM      CMD.EXE

C:\> 


So if we look back at our schedule expression "0 0 _ _ *", we will see that the job was scheduled correctly.
  • Minute=0 - top of the hour (minute zero)
  • Hour=0 - top of the day (hour zero)
  • day of month=_ - not used
  • month=_ - not used
  • day of week=* - any (M T W Th F S Su)
_________________________
Regards,
Luke Tomasello

Top
#1385 - 07/22/03 09:22 PM Re: Chapter 04 – Scheduling Remote Programs (AT)
Luke Tomasello Administrator Offline
Member

Registered: 09/17/00
Posts: 740
Loc: San Jose, CA., USA
Chapter 04 – Scheduling Remote Programs (AT), Part II

How 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.
Code:
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 Sub
This 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..
_________________________
Regards,
Luke Tomasello

Top


Moderator:  Luke Tomasello