Search code examples
phphtmlserver-sidewindows-task-scheduler

php server-side schtasks.exe command


I'm new to the site as a poster, but have used it for quite a while as I research new methods for my projects. I just can't seem to find the information I need for my current project, most likely because I'm not working my searches right. I'm hoping you guys can point me in a direction where I can get my answer.

Problem: I work for a company with user all around the globe and have to keep in mind that our global users cannot see all available data. With that in mind, I do have a system where some scheduled tasks are setup that some of our global users had setup and then the system was restricted to US only users. Once this restriction was placed on the system, these scheduled tasks are not accessible to the users and unfortunately that must remain on this system.

To address this, I have been tasked with setting up an internal web app that is accessible by our global users that will display the scheduled task information and allow the users to run any of the tasks manually if there is a need for it. Keep in mind, when the user runs the task, the command will need to come from the server hosting the site as it will be using a user account (Domain account) that has admin access to the server and can run the task as the global users have no access to this system.

I currently have most of this accomplished. I have a nice PHP page goes through an array filled with the Scheduled Tasks names, runs the SCHTASKS.EXE command (with necessary arguements and switches) on the server and retrieves the information for each Task. It then sorts the data and displays the needed data in a table format for the user to view. I also have a basic button being created for each task on each row of the table.

Here is the issue that I am running in. How do I configure this button, and PHP page, to tell the server to run the SCHTASKS.exe /RUN command (again, from the server and not the users system) to get the task to run?

Is this possible? I have worked on this for the last few hours just to hit a dead end?

I am fairly new to PHP, so I have been learning as I go.

Sorry for the long winded explanation, I just wanted to make clear what I'm trying to do here. I hope I didn't over explain anything.

Here's the code I have so far. Do I need to use AJAX or something else?

<?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>';

?>

Solution

  • Here's how I ended up resolving this. I am now able to get the button work as expected.

    <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>';
    
    ?>