简体   繁体   中英

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?

  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.

$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);
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM