Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Job schedule task inventory

by shonorio (Hermit)
on Jun 28, 2006 at 14:35 UTC ( #558029=sourcecode: print w/replies, xml ) Need Help??
Category: Win32
Author/Contact Info Solli Moreira Honorio (shonorioatperldotorgdotbr)
Description: Do a job inventory on all computers passed by command line, and output theses job information as tab format.
######################################################################
+##########
# Program     : Job Iventory
# Description : Do a job inventory on all computers passed by command 
+line, and
#               output theses job information as tab formated.
# Author      : Solli M. Honorio shonorio[at]perl[dot]org[dot]br
# License     : under the GPL (GNU Public License)
######################################################################
+##########

use Win32::TaskScheduler;
use strict;

######################################################################
+##########
######################## G L O B A L  V A R I A B L E S ##############
+##########
my %status_code = ( '267008' => 'Ready',
                    '267009' => 'Running',
                    '267010' => 'Disabled',
                    '267011' => 'Has not run',
                    '267012' => 'No more runs scheduled',
                    '267013' => 'Not scheduled',
                    '267014' => 'Terminated by User',
                    '267015' => 'No valid triggers',
                    '267016' => 'Event triggers dont have set run time
+s',
                    '267017' => 'Trigger not found',
                    '267018' => 'Properties not all set',
                    '267019' => 'No running task to terminate',
                    '267020' => 'Task scheduler not installed',
                    '267021' => 'Can not open task',
                    '267022' => 'Invalid task',
                    '267023' => 'No account information defined',
                    '267024' => 'Acount name not found',
                    '267025' => 'Account DB corrupt',
                    '267026' => 'Security services only on NT',
                    '267027' => 'Unknown object version' );

my %trigger_type = ( 0 => 'Once',
                     1 => 'Daily',
                     2 => 'Weekly',
                     3 => 'Monthly Date',
                     4 => 'Monthly Dow',
                     5 => 'On idle',
                     6 => 'At SystemStart',
                     7 => 'At Logon');

my %days_of_week = ( '01' => 'Sunday',
                     '02' => 'Monday',
                     '04' => 'Tuesday',
                     '08' => 'Wednesday',
                     '16' => 'Thursday',
                     '32' => 'Friday',
                     '64' => 'Saturday');

my %months       = ( '0001' => 'January',
                     '0002' => 'February',
                     '0004' => 'March',
                     '0008' => 'April',
                     '0016' => 'May',
                     '0032' => 'June',
                     '0064' => 'July',
                     '0128' => 'August',
                     '0256' => 'September',
                     '0512' => 'October',
                     '1024' => 'November',
                     '2048' => 'December');

my %which_week = (  1 => 'Fist week',
                    2 => 'Second week',
                    3 => 'Third week',
                    4 => 'Fourth week',
                    5 => 'Last week' );

######################## G L O B A L  V A R I A B L E S ##############
+##########
######################################################################
+##########

######################################################################
+##########
####################### D O   J O B   I N V E N T O R Y ##############
+##########
my %jobs_processed;
for ( @ARGV ) {
    if ( my @array = get_job_info ($_) ) {
        @{$jobs_processed{$_}} = @array;
    }
}
####################### D O   J O B   I N V E N T O R Y ##############
+##########
######################################################################
+##########

######################################################################
+##########
###################### O U T P U T   I N F O R M A T I O N ###########
+##########
print "Computer\t";
print "Job_Name\t";
print "Application_Name\t";
print "Parameters\t";
print "WorkDirectory\t";
print "Comment\t";
print "MostRecentRunTime\t";
print "NextRunTime\t";
print "MaxRunTime\t";
print "Priority\t";
print "Status\t";
print "User\t";
print "Creator\t";
print "Error\t";
print "Flag_Delete when done\t";
print "Flag_Disabled}\t";
print "Flag_Dont start if on batteries\t";
print "Flag_Hidden\t";
print "Flag_Interactive\t";
print "Flag_Kill if going on batteries\t";
print "Flag_Kill on idle end\t";
print "Flag_Restart on idle resume\t";
print "Flag_Run if connected to internet\t";
print "Flag_Run only if docked\t";
print "Flag_Start only if idle\t";
print "Flag_System required\t";
print "Trigger_String\t";
print "Trigger_Type\t";
print "Trigger_StartDay\t";
print "Trigger_EndDay\t";
print "Trigger_StartTime\t";
print "Trigger_Duration\t";
print "Trigger_Interval\t";
print "Trigger_Schedule_DaysInterval\t";
print "Trigger_Schedule_WeeksInterval\t";
print "Trigger_Schedule_WeeksDays\t";
print "Trigger_Schedule_Days\t";
print "Trigger_Schedule_WhichWeek\t";
print "Trigger_Schedule_Months\n";


foreach my $cp ( sort keys %jobs_processed ) {
    foreach my $job ( @{$jobs_processed{$cp}} ) {
        foreach my $idx ( sort keys %{$job->{Trigger}} ) {
            print "$cp\t";
            print "$job->{Name}\t";
            print "$job->{AppName}\t";
            print "$job->{Parameters}\t";
            print "$job->{WorkDirectory}\t";
            print "$job->{Comment}\t";
            print "$job->{MostRecentRunTime}\t";
            print "$job->{NextRunTime}\t";
            print "$job->{MaxRunTime}\t";
            print "$job->{Priority}\t";
            print "$job->{Status}\t";
            print "$job->{User}\t";
            print "$job->{Creator}\t";
            print "$job->{Error}\t";
            print "$job->{Flag}->{'Delete when done'}\t";
            print "$job->{Flag}->{Disabled}\t";
            print "$job->{Flag}->{'Dont start if on batteries'}\t";
            print "$job->{Flag}->{Hidden}\t";
            print "$job->{Flag}->{Interactive}\t";
            print "$job->{Flag}->{'Kill if going on batteries'}\t";
            print "$job->{Flag}->{'Kill on idle end'}\t";
            print "$job->{Flag}->{'Restart on idle resume'}\t";
            print "$job->{Flag}->{'Run if connected to internet'}\t";
            print "$job->{Flag}->{'Run only if docked'}\t";
            print "$job->{Flag}->{'Start only if idle'}\t";
            print "$job->{Flag}->{'System required'}\t";
            print "$job->{Trigger}{$idx}{String}\t";
            print "$job->{Trigger}{$idx}{Type}\t";
            print "$job->{Trigger}{$idx}{StartDay}\t";
            print "$job->{Trigger}{$idx}{EndDay}\t";
            print "$job->{Trigger}{$idx}{StartTime}\t";
            print "$job->{Trigger}{$idx}{Duration}\t";
            print "$job->{Trigger}{$idx}{Interval}\t";
            print "$job->{Trigger}{$idx}{Schedule}{DaysInterval}\t";
            print "$job->{Trigger}{$idx}{Schedule}{WeeksInterval}\t";
            print "$job->{Trigger}{$idx}{Schedule}{WeeksDays}\t";
            print "$job->{Trigger}{$idx}{Schedule}{Days}\t";
            print "$job->{Trigger}{$idx}{Schedule}{WhichWeek}\t";
            print "$job->{Trigger}{$idx}{Schedule}{Months}\n";
        }
    }
}
###################### O U T P U T   I N F O R M A T I O N ###########
+##########
######################################################################
+##########

sub get_job_info {
    my $computer_name = shift;
    my $scheduler     = Win32::TaskScheduler->New();
    my @jobs;
    
    if ( $scheduler->SetTargetComputer("\\\\$computer_name") ) {
        foreach my $job_name ( $scheduler->Enum() ) {
            my %job;
            $job_name  =~ s/\.job$//i;
            $job{Name} = $job_name;
        
            if ( $scheduler->Activate($job{Name}) ) {
                my ( $pri, $status, $error, $flags, $time_milli_second
+s );

                $scheduler->GetPriority($pri);
                $scheduler->GetStatus($status);
                $scheduler->GetExitCode($error);
                $flags = $scheduler->GetFlags();

                $job{Flag}{'Interactive'}                  = ( $flags 
+& $scheduler->TASK_FLAG_INTERACTIVE ) ? 1 : 0;
                $job{Flag}{'Delete when done'}             = ( $flags 
+& $scheduler->TASK_FLAG_DELETE_WHEN_DONE ) ? 1 : 0;
                $job{Flag}{'Disabled'}                     = ( $flags 
+& $scheduler->TASK_FLAG_DISABLED ) ? 1 : 0;
                $job{Flag}{'Start only if idle'}           = ( $flags 
+& $scheduler->TASK_FLAG_START_ONLY_IF_IDLE ) ? 1 : 0;
                $job{Flag}{'Kill on idle end'}             = ( $flags 
+& $scheduler->TASK_FLAG_KILL_ON_IDLE_END ) ? 1 : 0;
                $job{Flag}{'Dont start if on batteries'}   = ( $flags 
+& $scheduler->TASK_FLAG_DONT_START_IF_ON_BATTERIES ) ? 1 : 0;
                $job{Flag}{'Kill if going on batteries'}   = ( $flags 
+& $scheduler->TASK_FLAG_KILL_IF_GOING_ON_BATTERIES ) ? 1 : 0;
                $job{Flag}{'Run only if docked'}           = ( $flags 
+& $scheduler->TASK_FLAG_RUN_ONLY_IF_DOCKED ) ? 1 : 0;
                $job{Flag}{'Hidden'}                       = ( $flags 
+& $scheduler->TASK_FLAG_HIDDEN ) ? 1 : 0;
                $job{Flag}{'Run if connected to internet'} = ( $flags 
+& $scheduler->TASK_FLAG_RUN_IF_CONNECTED_TO_INTERNET ) ? 1 : 0;
                $job{Flag}{'Restart on idle resume'}       = ( $flags 
+& $scheduler->TASK_FLAG_RESTART_ON_IDLE_RESUME ) ? 1 : 0;
                $job{Flag}{'System required'}              = ( $flags 
+& $scheduler->TASK_FLAG_SYSTEM_REQUIRED ) ? 1 : 0;

                if ( $pri eq $scheduler->REALTIME_PRIORITY_CLASS ) {
                    $job{Priority} = 'Real time';
                } elsif ( $pri eq $scheduler->HIGH_PRIORITY_CLASS ) {
                    $job{Priority} = 'High';
                } elsif ( $pri eq $scheduler->NORMAL_PRIORITY_CLASS ) 
+{
                    $job{Priority} = 'Normal';
                } elsif ( $pri eq $scheduler->IDLE_PRIORITY_CLASS ) {
                    $job{Priority} = 'Idle';
                } else {
                    $job{Priority} = 'Unknown';
                }
            
                $job{User}              = $scheduler->GetAccountInform
+ation();
                $job{AppName}           = $scheduler->GetApplicationNa
+me();
                $job{Parameters}        = $scheduler->GetParameters();
                $job{WorkDirectory}     = $scheduler->GetWorkingDirect
+ory();
                $job{Status}            = $status_code{$status};
                $job{Error}             = $error;
                $job{Comment}           = $scheduler->GetComment();
                $job{Creator}           = $scheduler->GetCreator();
                $job{NextRunTime}       = format_hour($scheduler->GetN
+extRunTime());
                $job{MostRecentRunTime} = format_hour($scheduler->GetM
+ostRecentRunTime());

                if ( ( $time_milli_seconds = $scheduler->GetMaxRunTime
+() ) > -1 ) {
                    my ($hours, $minutes, $secounds);
                    $secounds = $time_milli_seconds / 1000;
                    $hours    = int ( $secounds / 3600 );
                    $minutes  = int ( ( $secounds - ( $hours * 3600 ) 
+) / 60 );
                    $secounds = int ( ( $secounds - ( ( $hours * 3600 
+) + ( $minutes * 60 ) ) ) );
                    $job{MaxRunTime} = sprintf "%d:%02d:%02d",$hours,$
+minutes,$secounds;
                }

                for ( my $idx = 0; $idx < $scheduler->GetTriggerCount(
+); $idx++ ) {
                    my ( %trigger );

                    $scheduler->GetTrigger( $idx, \%trigger );
                    $job{Trigger}{$idx}{String}    = $scheduler->GetTr
+iggerString($idx);
                    $job{Trigger}{$idx}{Type}      = $trigger_type{$tr
+igger{"TriggerType"}};
                    $job{Trigger}{$idx}{StartDay}  = sprintf "%04d-%02
+d-%02d", $trigger{BeginYear}, $trigger{BeginMonth}, $trigger{BeginDay
+};
                    $job{Trigger}{$idx}{EndDay}    = sprintf "%04d-%02
+d-%02d", $trigger{EndYear}, $trigger{EndMonth}, $trigger{EndDay};
                    $job{Trigger}{$idx}{StartTime} = sprintf "%02d:%02
+d", $trigger{StartHour}, $trigger{StartMinute};
                    $job{Trigger}{$idx}{Duration}  = sprintf "%02d:%02
+d", ( int $trigger{MinutesDuration} / 60 ),
                                                                      
+    ( $trigger{MinutesDuration} - ( ( int $trigger{MinutesDuration} /
+ 60 ) ) * 60 );
                    $job{Trigger}{$idx}{Interval}  = sprintf "%02d:%02
+d", ( int $trigger{MinutesInterval} / 60 ),
                                                                      
+    ( $trigger{MinutesInterval} - ( ( int $trigger{MinutesInterval} /
+ 60 ) ) * 60 );
                    if ( $trigger{"Type"} ) {
                        # build a schedule structure
                        if ( $trigger{'TriggerType'} == $scheduler->TA
+SK_TIME_TRIGGER_DAILY ) {
                            $job{Trigger}{$idx}{Schedule}{DaysInterval
+} = $trigger{'Type'}{DaysInterval};
                        } elsif ( $trigger{'TriggerType'} == $schedule
+r->TASK_TIME_TRIGGER_WEEKLY ) {
                            $job{Trigger}{$idx}{Schedule}{WeeksInterva
+l} = $trigger{'Type'}{WeeksInterval};
                            # do a loop and return the list of week da
+ys
                            $job{Trigger}{$idx}{Schedule}{WeeksDays}  
+   = join ',', map { ( $_ & $trigger{'Type'}{DaysOfTheWeek} ) ? $days
+_of_week{$_} : () } sort keys %days_of_week;
                        } elsif ( $trigger{"TriggerType"} == $schedule
+r->TASK_TIME_TRIGGER_MONTHLYDATE ) {
                            $job{Trigger}{$idx}{Schedule}{Days}   = $t
+rigger{'Type'}{Days};
                            # do a loop and return the list of months
                            $job{Trigger}{$idx}{Schedule}{Months} = jo
+in ',', map { ( $_ & $trigger{'Type'}{Months} ) ? $months{$_} : () } 
+sort keys %months;
                        } elsif ( $trigger{"TriggerType"} == $schedule
+r->TASK_TIME_TRIGGER_MONTHLYDOW ) {
                            $job{Trigger}{$idx}{Schedule}{WhichWeek} =
+ $which_week{$trigger{'Type'}{WhichWeek}}; 
                            $job{Trigger}{$idx}{Schedule}{WeeksDays} =
+ $days_of_week{$trigger{'Type'}{DaysOfTheWeek}};
                            # do a loop and return the list of months
                            $job{Trigger}{$idx}{Schedule}{Months} = jo
+in ',', map { ( $_ & $trigger{'Type'}{Months} ) ? $months{$_} : () } 
+sort keys %months;
                        }
                    }
                }
                push @jobs, \%job;
            } else {
                print STDERR "Could not activate the job $job{Name} at
+ computer $computer_name\n";
            }
        }
        $scheduler->End();
        return @jobs;
    } else {
        print STDERR "Could not connect the computer $computer_name\n"
+;
        return ();
    }
}

sub format_hour {
    my ($ms, $sec, $min, $hour, $day, $dayofweek, $month, $year) = @_;
    return sprintf "%04d-%02d-%02d %02d:%02d:%02d",$year,$month,$day,$
+hour,$min,$sec;
}
Replies are listed 'Best First'.
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2020-11-24 20:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?