php 服务器端 schtasks.exe 命令

php server-side schtasks.exe command

作为海报,我是该网站的新手,但在为我的项目研究新方法时已经使用它很长一段时间了。我似乎无法找到当前项目所需的信息,很可能是因为我没有正确进行搜索。我希望你们能指出我能得到答案的方向。

问题: 我在一家用户遍布全球的公司工作,必须记住,我们的全球用户无法看到所有可用数据。考虑到这一点,我确实有一个系统,其中设置了一些我们全球用户设置的计划任务,然后该系统仅限于美国用户。一旦对系统施加此限制,用户将无法访问这些计划任务,不幸的是,这些任务必须保留在此系统上。

为了解决这个问题,我的任务是设置一个可供我们全球用户访问的内部网络应用程序,该应用程序将显示计划任务信息并允许用户 运行 手动执行任何任务,如果这是有必要的。请记住,当用户 运行 执行任务时,命令将需要来自托管站点的服务器,因为它将使用对服务器具有管理员访问权限的用户帐户(域帐户)并且可以 运行 全局用户无法访问此系统的任务。

我目前已经完成了其中的大部分工作。我有一个很好的 PHP 页面遍历一个充满计划任务名称的数组,运行 在服务器上执行 SCHTASKS.EXE 命令(带有必要的参数和开关)并检索每个任务的信息任务。然后对数据进行排序,将需要的数据以table格式展示给用户查看。我还在 table.

的每一行上为每个任务创建了一个基本按钮

这是我要 运行 解决的问题。如何配置此按钮和 PHP 页面,以告诉服务器 运行 SCHTASKS.exe /运行 命令(同样,来自服务器而不是用户系统)将任务发送到 运行?

这可能吗?我在过去的几个小时里一直在研究这个,只是为了走到死胡同吗?

我是 PHP 的新手,所以我一直在学习。

抱歉冗长的解释,我只是想弄清楚我想在这里做什么。我希望我没有过度解释任何事情。

这是我目前的代码。我需要使用 AJAX 还是其他东西?

<?php
//Create colors for table
$success_color ="#E68080";         //row color when machine is occupied (light green)
$failed_color="#91FF6C";   //row color when machine is available (light red)

//Declare array with task names
$tasknames = array("\"TASK1\"", "\"TASK2\"");
//Declare array with TASK log network share
$tasklogs = array("\\Task1Log\share", "\\Task2Log\share");
//count the number in array
$numberoftasks = count($tasknames);

//Create table and column headers
echo '<table border ="1" cellpadding="5" cellspacing="0" style="border: 1px solid #c0c0c0;">';
echo '<tr>';
    echo '<th>Task Name</th>';
    echo '<th>Status</th>';
    echo '<th>Next Run Time</th>';
    echo '<th>Last Run Time</th>';
    echo '<th>Last Result</th>';
    echo '<th>Trigger</th>';
    echo '<th>Run As User</th>';
    echo '<th>Run Task</th>';
    echo '<th>Logs</th>';
echo '</tr>'; 
echo '<br><br>';

//Loop to create the rest of the table rows with Task information retrieved from system
for($x = 0; $x < $numberoftasks; $x++){
    $status = "0";
    $schtasksinst = "schtasks.exe /S SystemName /Query /TN $tasknames[$x] /FO LIST /V";
    $runtaskinst = "schtasks.exe /S SystemName /Run /TN $tasknames[$x]";
    exec($schtasksinst, $output);

//If statement to assign the appropriate color to a row based on if the task run successfully or had issues 
if ($status = (substr($output[8], 38))){
    $color = $success_color;
} else {
    $color = $failed_color;
}

//Fill table with Task info
    echo '<tr style="background: '.$color.';">';
    echo "<td>".substr($output[3], 39)."</td>";
    echo "<td>".substr($output[5],8)."</td>";
    echo "<td>".substr($output[4], 15)."</td>";
    echo "<td>".substr($output[7], 15)."</td>";
    echo "<td>0x".substr($output[8], 38)."</td>";
    echo "<td>".substr($output[20], 15)."</td>";
    echo "<td>".substr($output[16], 13)."</td>";
    echo "<td><button type=\"button\" onclick=\"???????????????????????????????\">Run Task</button></td>";
    echo "<td>".$tasklogs[$x]."</td>";
    unset($schtasksinst);
    unset($runtaskinst);
    unset($output);
}

\End table as all info has been displayed
echo '</table>';
echo '<br><br>';

\Create basic table as color legend
echo "<H2>Color Legend</H2>";
echo '<table border ="1" cellpadding="5" cellspacing="0" style="border: 1px solid #c0c0c0;">';
echo '<tr>';
echo '<td>Scheduled Task is either running or encountered an issue on it\'s last run:</th>';
echo '<td BGCOLOR=' . $success_color . '>' . str_repeat('&nbsp;', 30) . '</th>';
echo '</tr>';
echo '<tr>';
echo '<td>Scheduled task ran with no issues:</th>';
echo '<td BGCOLOR=' . $failed_color . '>' . str_repeat('&nbsp;', 30) . '</th>';
echo '</tr>';
echo '</table>';

?>

这是我最终解决这个问题的方法。我现在可以让按钮按预期工作。

<form action="runtask.php" method="post">

<?php
//Create colors for table
$success_color ="#E68080";         //row color when machine is occupied (light green)
$failed_color="#91FF6C";   //row color when machine is available (light red)

//Declare array with task names
$tasknames = array("\"TASK1\"", "\"TASK2\"");
//Declare array with TASK log network share
$tasklogs = array("\\Task1Log\share", "\\Task2Log\share");
//count the number in array
$numberoftasks = count($tasknames);

//Create table and column headers
echo '<table border ="1" cellpadding="5" cellspacing="0" style="border: 1px solid #c0c0c0;">';
echo '<tr>';
    echo '<th>Task Name</th>';
    echo '<th>Status</th>';
    echo '<th>Next Run Time</th>';
    echo '<th>Last Run Time</th>';
    echo '<th>Last Result</th>';
    echo '<th>Trigger</th>';
    echo '<th>Run As User</th>';
    echo '<th>Run Task</th>';
    echo '<th>Logs</th>';
echo '</tr>'; 
echo '<br><br>';

//Loop to create the rest of the table rows with Task information retrieved from system
for($x = 0; $x < $numberoftasks; $x++){
    $status = "0";
    $schtasksinst = "schtasks.exe /S SystemName /Query /TN $tasknames[$x] /FO LIST /V";
    $runtaskinst = "schtasks.exe /S SystemName /Run /TN $tasknames[$x]";
    exec($schtasksinst, $output);

//If statement to assign the appropriate color to a row based on if the task run successfully or had issues 
if ($status = (substr($output[8], 38))){
    $color = $success_color;
} else {
    $color = $failed_color;
}

//Fill table with Task info
    echo '<tr style="background: '.$color.';">';
    echo "<td>".substr($output[3], 39)."</td>";
    echo "<td>".substr($output[5],8)."</td>";
    echo "<td>".substr($output[4], 15)."</td>";
    echo "<td>".substr($output[7], 15)."</td>";
    echo "<td>0x".substr($output[8], 38)."</td>";
    echo "<td>".substr($output[20], 15)."</td>";
    echo "<td>".substr($output[16], 13)."</td>";
    echo "<td><input type=submit name=runtask value=\"Run $tasknames[$x]\"></td>";
    echo "<td>".$tasklogs[$x]."</td>";
    unset($schtasksinst);
    unset($runtaskinst);
    unset($output);
}

\End table as all info has been displayed
echo '</table>';
echo '<br><br>';

\Create basic table as color legend
echo "<H2>Color Legend</H2>";
echo '<table border ="1" cellpadding="5" cellspacing="0" style="border: 1px solid #c0c0c0;">';
echo '<tr>';
echo '<td>Scheduled Task is either running or encountered an issue on it\'s last run:</th>';
echo '<td BGCOLOR=' . $success_color . '>' . str_repeat('&nbsp;', 30) . '</th>';
echo '</tr>';
echo '<tr>';
echo '<td>Scheduled task ran with no issues:</th>';
echo '<td BGCOLOR=' . $failed_color . '>' . str_repeat('&nbsp;', 30) . '</th>';
echo '</tr>';
echo '</table>';

?>