Im trying to put together a script that will create a task scheduler entry that will reboot a machine 1x / week. I have it working where it will create the task scheduler entry but it's not getting formatted correctly in the resultant item. Since the command in question has spaces in it the task scheduler is putting quotes around the whole command. This is confusing the entry which uses 'perl -e' to run the command and requires quotes around its part. Confused yet? Here's the code:
#!/cygdrive/c/perl/bin/perl -w
use Win32::TaskScheduler;
use strict;
my $taskName = "weekly reboot";
my $APP_NAME = q#c:\\perl\\bin\\perl.exe -MWin32 -e "Win32::InitiateSy
+stemShutdown(' ','Rebooting',5,1,1);"#;
my $ACCOUNT_NAME = "bob";
my $ACCOUNT_PW = "jones";
my $scheduler = Win32::TaskScheduler->New();
$scheduler->Activate( $taskName );
#
# This adds a weekly schedule.
my %taskTrigger = (
'BeginYear' => 2007,
'BeginMonth' => 1,
'BeginDay' => 1,
'StartHour' => 1,
'StartMinute' => 0,
'TriggerType' => $scheduler->TASK_TIME_TRIGGER_WEEKLY,
'Type'=>{
'WeeksInterval' => 1,
'DaysOfTheWeek
+' => $scheduler->TASK_SUNDAY,
},
);
$scheduler->NewWorkItem( $taskName, \%taskTrigger );
$scheduler->SetApplicationName( $APP_NAME );
# run forever
$scheduler->SetMaxRunTime( $scheduler->INFINITE );
$scheduler->SetAccountInformation( $ACCOUNT_NAME, $ACCOUNT_PW );
$scheduler->Save();
I've tried various combinations of single and double quotes. If I create the task manually and put double quotes around the 'Win32::InitiateSystemShutdown' call it works.
addendum: task scheduler puts quotes around any command that has spaces in it... and doesn't work.. I tried putting 'shutdown.exe -r -t 5 -f' in there.
-
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.