Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Win32 Task Scheduler Monitor

by Courage (Parson)
on Jun 05, 2002 at 16:01 UTC ( #171882=note: print w/ replies, xml ) Need Help??


in reply to Win32 Task Scheduler Monitor

okay, there is much shorter to write, but longer to read docs way, using WMI (Windows Management Instrumentation), MSDN describes details, code and results are as following (just few lines of code! i did not expected to have such short program to do the trick...) :

use Win32::OLE 'in'; $objcoll = Win32::OLE->GetObject("WinMgmts://servername")->InstancesOf +("Win32_ScheduledJob"); my @x = qw/ Caption Command DaysOfMonth DaysOfWeek Description ElapsedTime Insta +llDate InteractWithDesktop JobId JobStatus Name Notify Owner Priority RunRe +peatedly StartTime Status TimeSubmitted UntilTime /; for $adsobj (in $objcoll) { print ++$i,".===\n",join '', map {" $_:".($adsobj->{$_}||"")."\n"} +@x; } ===== 1.=== Caption: Command:calc DaysOfMonth:16 DaysOfWeek: Description: ElapsedTime: InstallDate: InteractWithDesktop:1 JobId:2 JobStatus:Success Name: Notify: Owner: Priority: RunRepeatedly:1 StartTime:********202000.000000+240 Status: TimeSubmitted: UntilTime: 2.=== Caption: Command:calc DaysOfMonth:262144 DaysOfWeek: Description: ElapsedTime: InstallDate: InteractWithDesktop:1 JobId:3 JobStatus:Success Name: Notify: Owner: Priority: RunRepeatedly:1 StartTime:********202000.000000+240 Status: TimeSubmitted: UntilTime:
Note that GetObject("WinMgmts://servername") becomes GetObject("WinMgmts:") if you're working locally

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


Comment on Re: Win32 Task Scheduler Monitor
Download Code
Re: Win32 Task Scheduler Monitor
by Anonymous Monk on Jun 06, 2002 at 17:38 UTC
    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
      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://171882]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2014-04-20 22:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (489 votes), past polls