PHP |将参数传递到Powershell脚本中 [英] PHP | Passing parameters into Powershell script

查看:226
本文介绍了PHP |将参数传递到Powershell脚本中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个运行powershell脚本的PHP应用程序。

I am trying to create a PHP application which runs powershell scripts.

我有一个名为 change.ps1的文件,它是一个powershell脚本,这是该文件的内容:

I have a file called "change.ps1" which is a powershell script, here is the content of that file:

Param([string]$username, [string]$password)
Import-Module ActiveDirectory
$newpwd = ConvertTo-SecureString -String "$password" -AsPlainText –Force
Set-ADAccountPassword $username -NewPassword $newpwd –Reset

现在我有一个PHP页面,该页面使用shell_exec()函数运行此脚本并将两个参数传递给它,您可以在ps1文件中看到上面的内容。 ($ username,$ password):

Now i have a PHP page which uses the shell_exec() function to run this script and pass TWO parameters to it, which you can see above is used in the ps1 file ( $username, $password):

    $psscriptpath = "c:\inetpub\htdocsscripts\change.ps1";
shell_exec("powershell.exe -executionpolicy remotesigned -File" . $psscriptpath . " -username \"" . $username . "\" -password \"" . $password . "\"");

目前,这不起作用(页面永远也不会完成加载超时)。所以问题是,我正确地做到了吗?我哪里出错了。背后的理论相对简单,使用PHP将变量作为参数传递到Powershell脚本中。

At the moment this does not work (the page never finishes loading aka times out). So the question is, am i doing this correctly? where have i gone wrong. The theory behind this is relatively simple, use PHP to pass variables through as parameters into a powershell script.

感谢您的关注,如果您有任何疑问,请告诉我们。

Thanks for taking a look, and let me know if you have questions.

更新:
我尝试直接通过cmd运行脚本,以查看问题是否出在脚本本身中。这是显示给我的错误:
http://puu.sh/aXuH3/b8db154625.png

第二次更新:
我已经解决了CMD上的错误,这是一个编码问题。但是,当我尝试运行php页面时,仍然遇到超时问题:
http:/ /puu.sh/aXEdY/22cc87310c.png

推荐答案

这是Windows 2012R2服务器的解决方案-Apache 2.4 -PHP 7.4



技巧是在命令环境中切换代码页。下面显示了ps1文件中的UTF格式很好,因为 test.txt 的内容包含正确的数据。

This is a solution for Windows 2012R2 server - Apache 2.4 - PHP 7.4

The trick is to switch the codepage in the command environment. Below shows that UTF is well revieved inside the ps1 file, since the content of the test.txt holds the correct data.

当PowerShell的输出返回到代码页850中编码的服务器时,就会出现问题。

The problem arise, when the output from PowerShell goes back to the server encoded in codepage 850.

这是test.ps1

This is test.ps1

param([String]$p1)
$loc = $env:LOCALAPPDATA
"a${p1}b" | Out-File -FilePath "${loc}\test.txt" -Encoding utf8
chcp
chcp 65001
write-host "a${p1}b"
chcp 850

这是CDM.EXE的输出

This is output from CDM.EXE

C:\PowerShell>powershell -file test.ps1 -p1 ÆØŁódźÅ€
Active code page: 850
Active code page: 65001
aÆØŁódźÅ€b
Active code page: 850

这是PHP

echo "<pre>";
$psscriptpath = "C:/powershell/test.ps1 -p1 ÆØŁódźÅ€";
echo $psscriptpath . '<br>';
echo 'From PowerShell:<br>';
$psData = shell_exec("powershell.exe -executionpolicy remotesigned -File " . $psscriptpath);
echo $psData . '<br>';
echo 'From readfile: ';
readfile(getenv('LOCALAPPDATA') . "\\test.txt");
echo "</pre>";

这是从我的浏览器中获得的:

This is from my browser:

C:/powershell/test.ps1 -p1 ÆØŁódźÅ€
From PowerShell:
Active code page: 850
Active code page: 65001
aÆØŁódźÅ€b
Active code page: 850

From readfile: aÆØŁódźÅ€b

最后的评论:从某些应用程序生成数据后,请始终切换回本地化的代码页。

A final comment: Always switch back to you localized codepage after the data has been generated from some application.

我遇到了一些难以克服的琐碎事情在 65001模式下的CMD窗口

I have experienced stange things to hanppend in the CMD window when it is in 65001 mode

使用PostgreSQl psql.exe 输出数据。我认为对Mysql和其他人也一样。

This trick also works when using the PostgreSQl psql.exe to output data. I assume the same goes for Mysql and others.

这篇关于PHP |将参数传递到Powershell脚本中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆