简体   繁体   English

如何通过php禁用/启用计划任务

[英]How to Disable/Enable Scheduled task through php

I want to enable and disable scheduled task through my php code. 我想通过我的PHP代码启用和禁用预定任务。

I tried 我试过了

<?php
$cmd = "c:\\windows\\system32\\schtasks.exe /Change /TN openChrome /Enable";
$output = shell_exec($cmd);
echo $output;
?>

and

<?php
$cmd = "schtasks /Change /TN openChrome /Enable";
$output = shell_exec($cmd);
echo $output;
?>

These codes work fine when I write code for Run or End a task. 当我为运行或结束任务编写代码时,这些代码可以正常工作。 But to Enable or Disable tasks they do nothing. 但是要启用或禁用任务,它们什么也不做。

What is wrong in code to Enable/Disable? 启用/禁用代码有什么问题?

After spending almost 5 days on it I found a solution to do this. 花了将近5天后,我找到了解决方案。 To Enable/Disable task on Task scheduler you have to run Enable/Disable ("c:\\windows\\system32\\schtasks.exe /Change /TN openChrome /Enable") command on administrator privileges. 要在任务计划程序上启用/禁用任务,您必须以管理员权限运行启用/禁用(“ c:\\ windows \\ system32 \\ schtasks.exe / Change / TN openChrome / Enable”)命令。 One could run administrator commands by running command prompt as administrator. 可以通过以管理员身份运行命令提示符来运行管理员命令。 But it is difficult to run administrator commands from php. 但是很难从php运行管理员命令。 So for both, either you want to run cmd.exe commands or schtasks.exe commands as administrator from php, you just need to copy cmd.exe or schtasks.exe files from C:\\Windows\\System32 and paste to some other directory. 因此,对于这两种情况,您都想以管理员身份从php运行cmd.exe命令或schtasks.exe命令,只需从C:\\ Windows \\ System32复制cmd.exe或schtasks.exe文件,然后粘贴到其他目录中。 For Example, I paste that to D:\\ drive. 例如,我将其粘贴到D:\\驱动器。 Now right click on that. 现在右键单击它。 Click properties. 单击属性。 Go to Compatibility tab. 转到“兼容性”选项卡。 Check "Run this program as an administrator". 选中“以管理员身份运行该程序”。 Click OK. 单击确定。 Now go to your PHP file and use that path before command where you pasted cmd or schtasks file. 现在转到您的PHP文件,并在粘贴cmd或schtasks文件的命令之前使用该路径。 (ie, in my case I pasted SCHTASKS file in D:. So, now my command $cmd = "D:\\SCHTASKS /Change /TN openChrome /Enable"). (即,在我的情况下,我将SCHTASKS文件粘贴到D:中。因此,现在我的命令$ cmd =“ D:\\ SCHTASKS / Change / TN openChrome / Enable”)。

This method run all your SCHTASKS or cmd commands as administrator from PHP exec() or shell_exec() functions. 此方法以管理员身份从PHP exec()或shell_exec()函数运行所有SCHTASKS或cmd命令。

Note: The only purpose of coping file from C:\\Windows\\System32 is, in that folder you can't find compatibility tab in properties and there is no option to run always as administrator. 注意:从C:\\ Windows \\ System32处理文件的唯一目的是,在该文件夹中,无法在属性中找到“兼容性”选项卡,并且没有选项可以始终以管理员身份运行。 I don't know whether this method is secure or not. 我不知道这种方法是否安全。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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