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

时间:2017-09-26 19:49:10

标签: php powershell

我正在尝试用PHP编写一个基本网站,以便在幕后运行一些PowerShell任务。我的PHP文件如下所示:

<?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有一个更容易学习的曲线。

1 个答案:

答案 0 :(得分:0)

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