简体   繁体   中英

PHP | Passing parameters into Powershell script

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

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

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 . "\"");

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.

UPDATE: I tried running the script directly via the cmd to see if the problem was in the script its self. Here is the error i was shown: http://puu.sh/aXuH3/b8db154625.png

SECOND UPDATE: I have fixed the error on the CMD, it was an encoding issue. However when i try to run the php page, i still get the time out issue: http://puu.sh/aXEdY/22cc87310c.png

ok, seems you are missing something.

  1. you need to point to the absolute path of the script. If you saw my message on wizpert, i did a cd command to the directory of the script to ensure the correct environment:

    c:\\>cd c:\\my\\directory\\with\\powershells

then you can execute your powershell with all the variables on it. Also try to do it via cmd to see if it produces any messages. If you see there is a timeout, it means that the process or is hanging or the web user does not have enough rights to execute shells.

If so, modify your php.ini or whatever let it know that the www-data user (in apache, not sure on IIS) has rights to execute commands.\\

also it could be that you are executing that parameter wrongfully:

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

Make $password without quotes on the powerscript.

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.

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

A change to codepage 65001 (UTF8) is the solution.

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

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

This is 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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM