从 PHP 执行 Powershell 脚本

发布于 2024-10-23 00:14:42 字数 1585 浏览 5 评论 0原文

我正在尝试从 PHP 执行 powershell 脚本,但它似乎不起作用。

脚本“newEvent.ps1”在 Exchange 服务器上创建一个事件。

$psPath = "powershell.exe";
$psDIR = "C:\\wamp\\www\\ant\\assets\\ps\\";
$psScript = "newEvent.ps1";
$runScript = $psDIR. $psScript;
$runCMD = $psPath." ".$runScript." 2>&1"; 

echo "\$psPath  $psPath <br>";
echo "\$psDIR  $psDIR <br>";
echo "\$psScript  $psScript <br>";
echo "\$runScript  $runScript <br>";
echo "\$runCMD   $runCMD  <br>";

exec( $runCMD,$out,$ret);

echo "<pre>";
print_r($out);
print_r($ret);
echo "</pre>";

它输出:

$psPath powershell.exe
$psDIR C:\wamp\www\ant\assets\ps\
$psScript newEvent.ps1
$runScript C:\wamp\www\ant\assets\ps\newEvent.ps1
$runCMD powershell.exe C:\wamp\www\ant\assets\ps\newEvent.ps1 2>&1

Array
(
    [0] => File C:\wamp\www\ant\assets\ps\newEvent.ps1 cannot be loaded because the execut
    [1] => ion of scripts is disabled on this system. Please see "get-help about_signing"
    [2] => for more details.
    [3] => At line:1 char:39
    [4] => + C:\wamp\www\ant\assets\ps\newEvent.ps1 <<<<
    [5] =>     + CategoryInfo          : NotSpecified: (:) [], PSSecurityException
    [6] =>     + FullyQualifiedErrorId : RuntimeException
    [7] => 
)

如果我在命令行上运行 powershell.exe C:\wamp\www\ant\assets\ps\newEvent.ps1 ,它工作正常。

这是我第一次尝试这样的事情。我运行了 Set-ExecutionPolicy RemoteSigned -Scope LocalMachine 但它仍然给我同样的错误。 事实上,我运行了Set-ExecutionPolicy unristricted,但它仍然是一样的。

I'm trying to execute a powershell script from PHP, but it does not seem to work.

The script 'newEvent.ps1' creates an event on the Exchange server.

$psPath = "powershell.exe";
$psDIR = "C:\\wamp\\www\\ant\\assets\\ps\\";
$psScript = "newEvent.ps1";
$runScript = $psDIR. $psScript;
$runCMD = $psPath." ".$runScript." 2>&1"; 

echo "\$psPath  $psPath <br>";
echo "\$psDIR  $psDIR <br>";
echo "\$psScript  $psScript <br>";
echo "\$runScript  $runScript <br>";
echo "\$runCMD   $runCMD  <br>";

exec( $runCMD,$out,$ret);

echo "<pre>";
print_r($out);
print_r($ret);
echo "</pre>";

It outputs:

$psPath powershell.exe
$psDIR C:\wamp\www\ant\assets\ps\
$psScript newEvent.ps1
$runScript C:\wamp\www\ant\assets\ps\newEvent.ps1
$runCMD powershell.exe C:\wamp\www\ant\assets\ps\newEvent.ps1 2>&1

Array
(
    [0] => File C:\wamp\www\ant\assets\ps\newEvent.ps1 cannot be loaded because the execut
    [1] => ion of scripts is disabled on this system. Please see "get-help about_signing"
    [2] => for more details.
    [3] => At line:1 char:39
    [4] => + C:\wamp\www\ant\assets\ps\newEvent.ps1 <<<<
    [5] =>     + CategoryInfo          : NotSpecified: (:) [], PSSecurityException
    [6] =>     + FullyQualifiedErrorId : RuntimeException
    [7] => 
)

If I run powershell.exe C:\wamp\www\ant\assets\ps\newEvent.ps1 on the command-line, it works fine.

This is the first time im attempting something like this. I ran Set-ExecutionPolicy RemoteSigned -Scope LocalMachine but it still gives me the same error.
In fact I ran Set-ExecutionPolicy unristricted, but it's still the same.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

尐籹人 2024-10-30 00:14:42

看起来您的命令被单引号括起来。我想如果你删除它们,你的命令应该运行。

shell_exec 返回您运行的命令的输出。要进一步诊断,请将输出存储在变量中,然后将其打印出来:

$output = shell_exec($runCMD);
echo '<pre>' . $output . '</pre>';

确保启用运行脚本。该功能默认处于关闭状态。您必须在要运行 PowerShell 脚本的每台计算机上启用脚本执行。运行 about help_signing 了解更多信息。

Microsoft 建议运行 Set-ExecutionPolicy RemoteSigned -Scope LocalMachine。这允许计算机上的所有用户帐户毫无问题地运行本地脚本,但需要确认才能运行从互联网下载的脚本。这需要在管理提示符下运行。如果您运行的是 64 位操作系统,则需要从 64 位和 32 位 shell 执行此操作。

It looks like your command is surrounded by single-quotes. I think if you remove them, your command should run.

shell_exec returns the output from the command you run. To further diagnose, store the output in a variable, then print it out:

$output = shell_exec($runCMD);
echo '<pre>' . $output . '</pre>';

Make sure you enable running scripts. That capability is turned off by default. You have to enable the execution of scripts on each machine you want to run PowerShell scripts. Run about help_signing for more information.

Microsoft recommends running Set-ExecutionPolicy RemoteSigned -Scope LocalMachine. This allows all user accounts on a machine to run local scripts without issue, but requires confirmation to run scripts downloaded from the internet. This needs to be run in an administrative prompt. If you are running a 64-bit operating system, you'll need to do this from both a 64-bit and 32-bit shell.

终陌 2024-10-30 00:14:42

要从 PHP 执行脚本文件,您应该遵循以下示例:

您应该从简单的 PowerShell 脚本开始。创建一个名为“test.ps1”的文本文件。

现在在此文件中键入以下脚本:

Get-Process

将以下代码放入名为“test.php”的 PHP 文件中。请记住将以下示例中的文件路径 "C:/PATH/TO/test.ps1" 更新为您自己的脚本文件的绝对路径。

echo "<pre>";
echo Shell_Exec('powershell -InputFormat none -ExecutionPolicy ByPass -NoProfile -Command "& { . \"C:/PATH/TO/test.ps1\"; }"');
echo "</pre>";

上面的代码将输出所有正在运行的进程的列表。

请注意,我在 Windows PC 中运行脚本,并且由于文件路径而收到错误消息。因此,我用正斜杠替换了文件路径中的所有反斜杠。

以下参数值得注意:

  1. -InputFormat none - 由于 PowerShell 中的错误,脚本将永远不会完成运行或退出。此参数提供了解决此错误的方法。
  2. -ExecutionPolicy ByPass - 如果没有此参数,代码将抛出低权限错误消息。
  3. -NoProfile - 使用此参数,您的脚本将运行得更快、更可预测。
  4. -Command - 通过“-Command”参数而不是“-File”参数加载脚本文件,您将拥有更大的灵活性。
    例如,如果您需要调用脚本内的函数并向该函数发送参数,则需要使用“-Command”参数。

下面是如何在 PowerShell 脚本文件中调用函数 "my-function" 并传递参数 "-myParameter""-myOtherParameter" 的示例 分别具有值 "10""15" 的函数:

echo "<pre>";
echo Shell_Exec('powershell -InputFormat none -ExecutionPolicy ByPass -NoProfile -Command "& { . \"C:/PATH/TO/test.ps1\"; my-function -myParameter 10 -myOtherParameter 15 }"');
echo "</pre>";

To execute a script file from PHP you should follow this example:

You should start out with a simple PowerShell script. Create a text file with the name "test.ps1".

Now type the following script in this file:

Get-Process

Place the code below in a PHP file named "test.php". Remember to update the file path in the following example "C:/PATH/TO/test.ps1" with the absolute path to your own script file.

echo "<pre>";
echo Shell_Exec('powershell -InputFormat none -ExecutionPolicy ByPass -NoProfile -Command "& { . \"C:/PATH/TO/test.ps1\"; }"');
echo "</pre>";

The above code will output a list of all your running processes.

Note that I was running my script in a Windows PC and I was getting error messages because of the file path. So, I replaced all backslashes from the file path with forward slashes.

The following parameters are worthy of note:

  1. -InputFormat none - Thanks to a bug in PowerShell, the script will never finish running or exit. This parameter provides a workaround this bug.
  2. -ExecutionPolicy ByPass - Without this parameter, the code will throw a low privilege error message.
  3. -NoProfile - With this parameter, your script will run faster and more predictably.
  4. -Command - By loading your script file through the "-Command" parameter, instead of the "-File" parameter, you will have greater flexibility.
    If, for example, you needed to call a function inside your script and send parameters to that function, you would need to use the "-Command" parameter.

Here is an example of how to call a function "my-function" inside your PowerShell script file and pass the parameters "-myParameter" and "-myOtherParameter" to that function with the values "10" and "15" respectivelly:

echo "<pre>";
echo Shell_Exec('powershell -InputFormat none -ExecutionPolicy ByPass -NoProfile -Command "& { . \"C:/PATH/TO/test.ps1\"; my-function -myParameter 10 -myOtherParameter 15 }"');
echo "</pre>";
小…红帽 2024-10-30 00:14:42

将“-executionPolicy Unrestricted”与命令“powershell.exe”一起使用。因此命令将是:

powershell.exe -executionPolicy Unrestricted

那么它肯定会起作用。

Use "-executionPolicy Unrestricted" along with the command "powershell.exe". Therefore the command will be :

powershell.exe -executionPolicy Unrestricted

Then it will surely work.

去了角落 2024-10-30 00:14:42

在另一个网站上发现了这个,我想我会把它传递出去:

我正在调试一个使用 Windows API 的程序(创建一个子进程)
使用重定向的输入和输出进行处理)以捕获标准输出
Microsoft 的 Windows PowerShell。

传递给 PowerShell 的脚本(-文件开关)未执行并且
PowerShell 刚刚挂起,直到被任务管理器终止。

原来你需要使用未记录的参数“-InputFormat
无”:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -InputFormat none -File file.ps1

这对我来说很有效。

Found this on another website and thought I would pass it along:

I was debugging a program that uses Windows API (Creating a Child
Process with Redirected Input and Output) to capture stdout of
Microsoft’s Windows PowerShell.

Script passed to PowerShell (-File switch) didn’t execute and
PowerShell just hanged until killed by Task Manager.

It turns out that you need to use undocumented parameter “-InputFormat
none”:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -InputFormat none -File file.ps1

This did the trick for me.

如梦 2024-10-30 00:14:42

或者,您可以更改 FROM: $psPath = "powershell.exe";至:$psPath =“pwsh.exe”;

Or, you can just change FROM: $psPath = "powershell.exe"; TO: $psPath = "pwsh.exe";

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文