2

I want to create a scheduled task using C:\Windows\System32\schtasks.exe in a perl script. I'm using the following script:

use strict;
my $createScheduleTask = `SchTasks /Create /SC WEEKLY /D MON,TUE,WED,THU,FRI /TN \"updater\" /TR \"\\\\network\\shared\\updater.bat\" /ST 19:00`;

But I get the following response:

ERROR: Invalid syntax.
Type "SCHTASKS /?" for usage.

When I run the same command in the CLI it works fine (I did remove the escape '\'-s.) I've tried about every possible combination of escape characters, but I cannot find out what is wrong.

So does anybody know what is wrong or does anybody know how to debug it?

Thanks
Rudy
PS I don't want to use Win32::TaskScheduler since it is not installed on all clients.

toolic
  • 57,801
  • 17
  • 75
  • 117
Rudy
  • 41
  • 4
  • 3
    Your code worked fine at my end. I got the following output: `C:\Users\Chankey\Desktop>perl test.pl SUCCESS: The scheduled task "updater" has successfully been created.` – Chankey Pathak Oct 23 '15 at 14:23
  • Works here, cut and paste error? Permissions problem? – Dr.Avalanche Oct 23 '15 at 15:03
  • Change `my $createScheduleTask = \`...\`;` to `my $cmd = qq\`...\`; my $createScheduleTask = \`$cmd\`;`, so you can print the command you actually ran and find the syntax error. – ikegami Oct 23 '15 at 15:04
  • Ok, on two other machines the script works fine. So I will be blaming it on 'some' machine configuration. Thanks guys. – Rudy Oct 27 '15 at 06:58

1 Answers1

1

This is likely due to the shell that you are running the command from.

I had this problem when running from a bash shell, but when I opened a standard Windows command prompt it runs fine.

Simpler
  • 1,317
  • 2
  • 16
  • 31
  • I had the same problem. I was trying to run it through Git's bash shell. Running it through the standard CMD shell solved the problem. Thank you! – manu3d Jun 25 '19 at 08:54