PHP/Windows 任务计划程序 - 如何从 php 创建新任务?

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

我有以下代码片段,我试图修改它以在 php 中的 windows 中创建计划任务。我试过 exec,然后 pclose(popen($cmd)) 但没有成功。 php 脚本执行但没有调用任何命令,我在我的任务计划程序 gui 中看不到添加的计划任务。

问题

如何从 php 调用 schtasks.exe 来创建新任务?

代码段

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

编辑

问题本地化

Apache 错误日志显示此消息

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:

我需要做什么来解决这个问题?

  1. 您需要有一个用户。所以你应该添加 /RU "username".
    我建议 运行 任务作为 system.

  2. 您不需要完整地址。
    "c:\windows\system32\schtasks.exe" >> schtasks.exe

  3. 您可以在 windows 的命令行末尾使用“>your file.txt”获得反馈。
    exp: 目录 > "c:\Directories.txt"

你的新鳕鱼:

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

祝你好运。

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

我想补充一点 如果您将 IIS 用于您的 PHP 服务器,您需要将管理员用户添加到身份验证的 IIS 文件夹部分。 你可以使用 exec() 而不是 pclose(popen()) 函数