3

I have the following code snippet which I have tried to modify to create a scheduled task in windows from php. I tried exec, then pclose(popen($cmd)) with no success. The php script executes but no command is invoked and I see no added scheduled task in my Task Scheduler gui.

Question

How can I invoke schtasks.exe from php to create a new task?

Code Snippet

    $daysList = join(", ", $days);


    $cmd = "c:\\windows\\system32\\schtasks.exe /CREATE /SC WEEKLY /D \"$daysList\" /TN \"Action Item Reminder\" /TR \"php.exe  C:\\wamp\\www\\aim\\module\\Application\\src\\Application\\Controller\\sendmail.php\" /ST 00:01 /f";

    pclose(popen("start /B ". $cmd, "r"));  


    //echo "c:\\windows\\system32\\schtasks.exe /CREATE /SC WEEKLY /D \"$daysList\" /TN \"Action Item Reminder\" /TR \"C:\\wamp\\bin\\php\\php5.5.12\\php.exe  C:\\wamp\\www\\aim\\module\\Application\\src\\Application\\Controller\\sendmail.php\" /ST 00:01 /f";

    //echo '/CREATE /SC WEEKLY /D "'.  $daysList .'" /TN "Action Item Reminder" /TR "C:\wamp\www\aim\module\Application\src\Application\Controller\sendmail.php" /ST 00:01 /f"'; die();

    if (isset ($activate))
    {
        $emailOptionTable->update('true', 'Activate Reminders');
        $cmd = "c:\\windows\\system32\\schtasks.exe /Change /TN \"Action Item Reminder\" /Enable";

        pclose(popen("start /B ". $cmd, "r"));  
    }
    else
    {
        $emailOptionTable->update('false', 'Activate Reminders');   
        $cmd = "c:\\windows\\system32\\schtasks.exe /Change /TN \"Action Item Reminder\" /Disable";

        pclose(popen("start /B ". $cmd, "r"));
    }

EDIT

Localization of Issue

Apache Error Log Shows This Message

ERROR: No mapping between account names and security IDs was done.

(46,4):UserId:ERROR: No mapping between account names and security IDs was done.

(46,4):UserId:

What do I need to do to resolve this issue?

Vahe
  • 1,699
  • 3
  • 25
  • 76

3 Answers3

5
  1. You need to have a user. So you should add /RU "username".
    I suggest running tasks as system.

  2. You do not need to full address.
    "c:\windows\system32\schtasks.exe" >> schtasks.exe

  3. You can get feedback from command line in windows with use ">your file.txt" at end of line.
    exp: dir > "c:\Directories.txt"

Your New Cod:

    $daysList = join(", ", $days);

    $cmd = "schtasks.exe /CREATE /SC WEEKLY /D \"$daysList\" /TN \"Action Item Reminder\" /TR \"php.exe  C:\\wamp\\www\\aim\\module\\Application\\src\\Application\\Controller\\sendmail.php\" /ST 00:01 /f /RU System";

    pclose(popen("start /B ". $cmd, "r")); // OR exec($cmd);

    //echo "schtasks.exe /CREATE /SC WEEKLY /D \"$daysList\" /TN \"Action Item Reminder\" /TR \"C:\\wamp\\bin\\php\\php5.5.12\\php.exe  C:\\wamp\\www\\aim\\module\\Application\\src\\Application\\Controller\\sendmail.php\" /ST 00:01 /f /RU System";
    //echo '/CREATE /SC WEEKLY /D "'.  $daysList .'" /TN "Action Item Reminder" /TR "C:\wamp\www\aim\module\Application\src\Application\Controller\sendmail.php" /ST 00:01 /f /RU System"'; die();

    $cmd ="schtasks.exe /Change /TN \"Action Item Reminder\" /RU System";
    if (isset ($activate))
    {
        $emailOptionTable->update('true', 'Activate Reminders');
        pclose(popen("start /B ". $cmd." /Enable", "r")); // OR exec($cmd);
    }
    else
    {
        $emailOptionTable->update('false', 'Activate Reminders'); 
        pclose(popen("start /B ". $cmd." /Disable", "r")); // OR exec($cmd);
    }

Good luck.

Amin Maleki
  • 156
  • 2
  • 4
  • 19
1

$daysList = join(", ", $days);

$cmd = "schtasks.exe /CREATE /SC WEEKLY /D \"$daysList\" /TN \"Action Item Reminder\" /TR \"php.exe  C:\\wamp\\www\\aim\\module\\Application\\src\\Application\\Controller\\sendmail.php\" /ST 00:01 /f /RU System";

pclose(popen("start /B ". $cmd, "r")); // OR exec($cmd);

//echo "schtasks.exe /CREATE /SC WEEKLY /D \"$daysList\" /TN \"Action Item Reminder\" /TR \"C:\\wamp\\bin\\php\\php5.5.12\\php.exe  C:\\wamp\\www\\aim\\module\\Application\\src\\Application\\Controller\\sendmail.php\" /ST 00:01 /f /RU System";
//echo '/CREATE /SC WEEKLY /D "'.  $daysList .'" /TN "Action Item Reminder" /TR "C:\wamp\www\aim\module\Application\src\Application\Controller\sendmail.php" /ST 00:01 /f /RU System"'; die();

$cmd ="schtasks.exe /Change /TN \"Action Item Reminder\" /RU System";
if (isset ($activate))
{
    $emailOptionTable->update('true', 'Activate Reminders');
    pclose(popen("start /B ". $cmd." /Enable", "r")); // OR exec($cmd);
}
else
{
    $emailOptionTable->update('false', 'Activate Reminders'); 
    pclose(popen("start /B ". $cmd." /Disable", "r")); // OR exec($cmd);
}
suri bai
  • 11
  • 1
  • 1
    From Review:  Hi, please don't answer just with source code. Try to provide a nice description about how your solution works. See: [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). Thanks – sɐunıɔןɐqɐp Oct 10 '19 at 13:37
0

I want to add an important point if you use IIS for your PHP server you need to add an administrator user to your IIS folder section of authentication. and you can use exec() instead of pclose(popen()) function

rezaSefiddashti
  • 134
  • 1
  • 9