PHP shell_exec需要30秒才能运行基本的PowerShell脚本

问题描述:

我想在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

我最终将网站迁移到IIS而不是Apache,并且代码在几秒钟内执行。它必须是Apache和PowerShell的问题。