Dear Courage, I wolud love to appreciate for all you have done. After tried the WMI approach, it can get all AT instances, but can't see any instance created by Task Scheduler UI program. However, your MSDN research lights me up. After several search in MSDN, I found COM objects called Task Scheduler API and one sample C++ code. It works on instances created by Task Scheduler UI Program. Then, I found another Perl extension Win32-TaskScheduler, created by Umberto Nicoletti. You can find it at http://taskscheduler.sourceforge.net. I'm not familiar with Perl, COM, OLE, but still tried to build a simple example querying the status of remote machine's Task Scheduler instances. Attached for your reference: Thank you very much!!
use strict;
use Win32::TaskScheduler;
(my $Machine = shift @ARGV || "" ) =~ s/^[\\\/]+//;
my $scheduler;
my @jobs;
my $job;
my $jobname;
my $account;
my $APname;
my $parameter;
my $workdir;
my $triggerCnt;
my $idx;
my %trigger;
my $key;
my $trigType;
my $flags;
my $status;
my $exitcode;
$scheduler = Win32::TaskScheduler->New();
if ( $Machine ) {
$scheduler->SetTargetComputer("\\\\".$Machine) or die("Failed: Set
+ Server Machine:".$Machine."\n");
}
@jobs = $scheduler->Enum();
foreach $job ( @jobs ) {
$jobname = substr($job,0,length($job)-4);
$scheduler->Activate($jobname) or die("Failed: Activate".$job." ["
+.$jobname."]\n");
print "==>".$job." [".$jobname."]\n";
$APname = $scheduler->GetApplicationName();
if ( $APname ) {print "\tAPname:".$APname."\n";}
$parameter = $scheduler->GetParameters();
if ( $parameter ) {print "\tParameter:".$parameter."\n";}
$workdir = $scheduler->GetWorkingDirectory();
if ( $workdir ) {print "\tWorkDir:".$workdir."\n";}
$account = $scheduler->GetAccountInformation();
if ( $account ) {print "\tAccount:".$account."\n";}
$scheduler->GetExitCode($exitcode);
print "\tExitCode:".$exitcode."\n";
$scheduler->GetStatus($status);
if ( $status == 267008 ) { #Ready
print "\tStatus:ready\n";
}
elsif ( $status == 267009 ) { #Runnig
print "\tStatus:RUNNING\n";
}
elsif ( $status == 267010 ) { #Not Scheduled
print "\tStatus:Not Scheduled\n";
}
else {
print "\tStatus:UNKNOWN\n";
}
$flags = $scheduler->GetFlags();
if ( $flags ) {print "\tFlags:".$flags."\n";}
$triggerCnt = $scheduler->GetTriggerCount();
if ( $triggerCnt > 0 ) {
for ( $idx = 0; $idx < $triggerCnt; $idx++ ) {
print "\tTrigger $idx:\n";
$scheduler->GetTrigger($idx,\%trigger);
foreach $key (keys %trigger) {
if ( $key eq "TriggerType" ) {
$trigType = $trigger{$key};
if ( $trigType == $scheduler->TASK_TIME_TRIGGER_ON
+CE ) {
print "\t\t$key=ONCE\n";
}
elsif ( $trigType == $scheduler->TASK_TIME_TRIGGER
+_DAILY ) {
print "\t\t$key=DAILY\n";
}
elsif ( $trigType == $scheduler->TASK_TIME_TRIGGER
+_WEEKLY ) {
print "\t\t$key=WEEKLY\n";
}
elsif ( $trigType == $scheduler->TASK_TIME_TRIGGER
+_MONTHLYDATE ) {
print "\t\t$key=MONTHLY_DATE\n";
}
elsif ( $trigType == $scheduler->TASK_TIME_TRIGGER
+_MONTHLYDOW ) {
print "\t\t$key=MONTHLY_DOW\n";
}
elsif ( $trigType == $scheduler->TASK_EVENT_TRIGGE
+R_ON_IDLE ) {
print "\t\t$key=ON_IDLE\n";
}
elsif ( $trigType == $scheduler->TASK_EVENT_TRIGGE
+R_AT_SYSTEMSTART ) {
print "\t\t$key=AT_SYSTEMSTART\n";
}
elsif ( $trigType == $scheduler->TASK_EVENT_TRIGGE
+R_AT_LOGON ) {
print "\t\t$key=AT_LOGON\n";
}
else {
print "\t\t$key=".$trigger{$key}."\n";
}
}
else {
print "\t\t$key=".$trigger{$key}."\n";
}
}
}
}
}
$scheduler->End();
Command Line:
*************
perl -w sched.pl twjimmywang
Sample Output:
**************
==>NetLinkMonitor.job [NetLinkMonitor]
APname:D:\MRTG\NetLinkMonitor\NetUp.exe
WorkDir:D:\MRTG\NetLinkMonitor
Account:TW01\adm
ExitCode:0
Status:ready
Flags:192
Trigger 0:
MinutesInterval=0
Flags=0
StartMinute=0
EndMonth=0
BeginDay=29
RandomMinutesInterval=0
BeginMonth=4
TriggerType=AT_SYSTEMSTART
EndYear=0
StartHour=0
MinutesDuration=0
BeginYear=2002
EndDay=0
==>Mrtg.job [Mrtg]
APname:D:\MRTG\CFG\Mrtg.bat
WorkDir:D:\MRTG\CFG
Account:TW01\adm
ExitCode:0
Status:ready
Flags:192
Trigger 0:
MinutesInterval=0
Flags=0
StartMinute=0
EndMonth=0
BeginDay=14
RandomMinutesInterval=0
BeginMonth=3
TriggerType=AT_SYSTEMSTART
EndYear=0
StartHour=0
MinutesDuration=0
BeginYear=2002
EndDay=0
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|