Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP/Windows Task Scheduler - How to create a new task from php?

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?

like image 505
Vahe J Avatar asked Apr 10 '26 04:04

Vahe J


1 Answers

  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.

like image 105
Amin Maleki Avatar answered Apr 12 '26 16:04

Amin Maleki



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!