Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl Win32 Task Scheduler module

Status
Not open for further replies.

BobSakemano

Technical User
Feb 17, 2005
20
0
0
NL
I need to do some scripting with the Windows Task Scheduler. After some searching I found the Perl Win32 Task Scheduler module.

What I need to do is the following: I need to enable and disable a scheduled task in windows via a command or in a script.
 
Please see thread219-1010407 for a discussion on creating a Windows Service.
 
I've done a little work with the module you are speaking of. See if the following code will help:
Code:
#!/usr/bin/perl

use strict;
use warnings;

use Win32::TaskScheduler;
use File::Basename;

my $scheduler = Win32::TaskScheduler->New();
# Name our expected tasks
my $batch1 = "BATCH1";
my $batch2 = "BATCH2";
print "... Starting scheduled tasks maintenance script\n";
&findold;
my $wait;
# -------------------------------------------------------
# findold sub: Check for old scheduled tasks, deleting the 
# expected ones.  If unexpected tasks are found, find any
# details about them and report.
# -------------------------------------------------------
sub findold {
my $result;
print "Checking for old scheduled tasks... ";
my @oddjobs;
my @jobs = $scheduler->Enum();
my $job_amt = scalar(@jobs);
print "found $job_amt\n";
foreach my $task(@jobs) {
my ($name, $path, $suffix) = fileparse($task, '\.[^\.]*');

   if ($name eq $batch2 || $name eq $batch1) {
	   print "Deleting expected task: $task ...";
	   $scheduler->Delete($task);
	   print " deleted\n";
   }
   else {  
	   push(@oddjobs, $name);
   }
}
 my $odd_amt = scalar(@oddjobs);
   if($odd_amt > 0) {	
   	print "Found $odd_amt of unexpected job(s), trying to retrieve more details...\n";	        
	foreach my $odd_task(@oddjobs) { 
	$result = $scheduler->Activate($odd_task);
	if ($result == 0) { print "Failed to activate $odd_task\n"; }
	my $ap = $scheduler->GetApplicationName();
	print "*#-(APPLICATION NAME)-#*\n";
	print "   $odd_task\n";
	print "*#-(APPLICATION LOCATION)-#*\n";
	print "   $ap\n\n";
	
	$result = $scheduler->Save();
	if ($result == 0) { print "Failed to save $odd_task\n"; }
	}
   }


&tgr;
&cspr;
}

# -------------------------------------------------------
# tgr sub: Set up the batch1 process
# -------------------------------------------------------
sub tgr {
print "Setting up new $batch1 task...";
my %batch1_trig=(
		'BeginYear' => 2005,
		'BeginMonth' => 01,
		'BeginDay' => 13,
		'StartHour' => 23,
		'StartMinute' => 00,
		'TriggerType' => $scheduler->TASK_TIME_TRIGGER_DAILY,
		'Type'=>{
			'DaysInterval' => 1,
		},
	);

my $batch1_app = "c:/BATCH.EXE";
my $arg = " ";
my $batch1_dir = 'c:\BATCH';
my $result;

$result = $scheduler->NewWorkItem($batch1, \%batch1_trig);
if ($result == 0) { print "Failed to add NewWorkItem for $batch1\n"; }
$result = $scheduler->SetApplicationName($batch1_app);
if ($result == 0) { print "Failed to SetApplicationName $batch1_app\n"; }
$result = $scheduler->SetWorkingDirectory($batch1_dir);
if ($result == 0) { print "Failed to SetWorkingDirectory $batch1_dir\n"; }
# Edit the following line with your own credentials
$result = $scheduler->SetAccountInformation('USERNAME','PASSWORD');
if ($result == 0) { print "Failed to SetAccountInformation for $batch1\n"; }
$result = $scheduler->SetPriority($scheduler->HIGH_PRIORITY_CLASS);
if ($result == 0) { print "Failed to SetPriority for $batch1\n"; }

$result = $scheduler->Save();
if ($result == 0) { print "Failed to save $batch1\n"; }
print " done\n";
}

# -------------------------------------------------------
# cspr sub: Set up the batch2 process
# -------------------------------------------------------
sub cspr {
print "Setting up new $batch2 task...";	
my %batch2_trig=(
               'BeginYear' => 2005,
               'BeginMonth' => 01,
               'BeginDay' => 13,
               'StartHour' => 05,
               'StartMinute' => 00,
               'TriggerType' => $scheduler->TASK_TIME_TRIGGER_DAILY,
               'Type'=>{
                       'DaysInterval' => 1,
               },
        );

my $batch2_app="c:/batch2.bat";
my $arg=" ";
my $batch2_dir= 'c:/BATCH';
my $result;

$result = $scheduler->NewWorkItem($batch2,\%batch2_trig);
if ($result == 0) { print "Failed to add NewWorkItem for $batch2\n"; }
$result = $scheduler->SetApplicationName($batch2_app);
if ($result == 0) { print "Failed to SetApplicationName $batch2_app\n"; }
$result = $scheduler->SetWorkingDirectory($batch2_dir);
if ($result == 0) { print "Failed to SetWorkingDirectory $batch2_dir\n"; }
# Edit the following line with your own credentials
$result = $scheduler->SetAccountInformation('USERNAME','PASSWORD');
if ($result == 0) { print "Failed to SetAccountInformation for $batch2\n"; }
$result = $scheduler->SetPriority($scheduler->HIGH_PRIORITY_CLASS);
if ($result == 0) { print "Failed to SetPriority for $batch2\n"; }

$result=$scheduler->Save();
if ($result == 0) { print "Failed to save $batch2\n"; }
print " done\n";

}

system('pause');

Let us know your results!

X
 
hmm, I need a little bit more than that guys. My experiance with Perl is very limited...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top