http://www.perlmonks.org?node_id=172291


in reply to Re: Win32 Task Scheduler Monitor
in thread Win32 Task Scheduler Monitor

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

Replies are listed 'Best First'.
Re: Re: Win32 Task Scheduler Monitor
by Courage (Parson) on Jun 07, 2002 at 05:47 UTC
    You're right. I forgot to try creating tasks via GUI.
    Anyway, it was good exercise for me to try.

    Best wishes,
    Courage, the Cowardly Dog.
    PS. Something fishy is going on there, or my name is Vadim Konovalov. And it's not.