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?
You need to have a user. So you should add /RU "username".
I suggest running tasks as system.
You do not need to full address.
"c:\windows\system32\schtasks.exe" >> schtasks.exe
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.