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.
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:
Make $password without quotes on the powerscript.