#!/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');