2015-02-24 64 views
0

我是新來的網站作爲海報,但已經使用了很長一段時間,因爲我研究了我的項目的新方法。我似乎無法找到我目前需要的信息,很可能是因爲我沒有正確地進行搜索。我希望你們能指出我的方向,我可以得到我的答案。php服務器端schtasks.exe命令

問題: 我爲一家全球用戶的公司工作,必須記住,我們的全球用戶無法看到所有可用數據。考慮到這一點,我確實有一個系統,在這個系統中,我們的一些全球用戶已經設置了一些計劃任務,然後系統僅限於美國用戶。一旦將此限制放置在系統上,用戶就無法訪問這些計劃任務,並且不幸的是,這些任務必須保留在此係統上。

爲了解決這個問題,我的任務是設置一個內部Web應用程序,我們的全球用戶可以訪問該應用程序,該應用程序將顯示計劃的任務信息,並允許用戶在需要時手動運行任何任務它。請記住,當用戶運行任務時,該命令需要來自託管該站點的服務器,因爲它將使用具有管理員訪問服務器的用戶帳戶(域帳戶),並且可以作爲全局運行該任務用戶無法訪問此係統。

我目前已經完成了大部分工作。我有一個很好的PHP頁面,通過一個填充了Scheduled Tasks名稱的數組,在服務器上運行SCHTASKS.EXE命令(包含必要的參數和切換)並檢索每個Task的信息。然後,它會對數據進行排序並以表格格式顯示所需數據供用戶查看。我還爲表格的每一行上的每個任務創建了一個基本按鈕。

這是我運行的問題。如何配置此按鈕和PHP頁面以告訴服務器運行SCHTASKS.exe/RUN命令(再次從服務器而不是用戶系統)讓任務運行?

這可能嗎?在過去的幾個小時裏,我已經爲此工作過,只是爲了達到死衚衕?

我對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>'; 

?> 
+0

嗯,我應該在運行此命令的服務器上設置一個腳本嗎?我希望能在PHP中做到這一切,但我願意提供建議! – Steve 2015-02-24 17:16:34

+0

您需要創建一個html表單並將該表單「發佈」到另一個php頁面,這將運行您的服務器端命令:) – britter 2015-02-24 17:26:32

+0

我試過這個,我用下面的方法構建按鈕:echo「​​

「;; .....我想知道如果我建立表的方式是這個問題?另一個PHP文件運行exec(schtasks.exe ....和其餘的命令)。 – Steve 2015-02-24 18:32:11

回答

1

以下是我最終解決這個問題的方法。我現在能夠按預期的方式獲得按鈕。

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

?>