2017-09-26 75 views
0

我想在PHP中編寫一個基本網站來在後臺運行一些PowerShell任務。我的PHP文件看起來像這樣:PHP shell_exec需要30秒才能運行基本的PowerShell腳本

<?php 
session_start(); 
?> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<title>Maintenance Page</title> 
</head> 
<body> 
<?php 


// Path to the PowerShell script. Remember double backslashes: 
$psScriptPath = "C:\\xampp\\htdocs\\SetAHMaintModeON.ps1"; 

// Execute the PowerShell script, passing the parameters: 
$query = shell_exec("powershell -command $psScriptPath"); 
echo "Maintenance Mode is now enabled!"; 



?> 

<form method="post" action="home.php"> 
<input type="submit" name="returnHome" value="Home" <?php 
$_SESSION["siteState"] = "red"; ?>> 
</form> 
</body> 
</html> 

而且,這是獲得所謂的PowerShell代碼:

#Import Modules 

Import-Module F5 -ErrorAction Stop -Global 

#Declare variables 

$LtmOne = "myltm.com" 
$DataGroupName = "Maintenance_Mode" 
$State ="off" 

#Connect to F5 LTMs and set Maintenance Mode to on 

Connect-F5 $LtmOne 
Set-MaintenanceMode -Name $DataGroupName -State $State -Confirm:$false 

如果我運行此腳本PHP之外,它秒內執行。但是,只要我把它放在PHP後面,執行就會開始。我有沒有代碼效率低下?我絕不是PHP開發專家,所以它的可能的錯誤代碼正在減慢速度。我唯一的選擇是在.NET中重寫這一切,我不希望這樣做,因爲我發現PHP有一個更容易的學習曲線。

+0

是否「需要30秒才能運行」,或者是否在30秒後超時,並且您不知道它是否運行? – TessellatingHeckler

+0

是的,它確實運行 – jnunham

回答

0

我最終將網站遷移到IIS而不是Apache,並且代碼在幾秒鐘內執行。它必須是Apache和PowerShell的問題。

相關問題