Executing a Powershell script from PHP
Asked Answered
C

5

13

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.

Coleorhiza answered 15/3, 2011 at 20:1 Comment(1)
Make sure to go into both the 32-bit and 64-bit instances and set the execution policy then retry. -> Set-ExecutionPolicy UnrestrictedMagniloquent
U
11

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.

Undercool answered 15/3, 2011 at 23:19 Comment(1)
ok figured it out #4647929 need to run it on both x86 and 64 bit consoles i was only doing it on the 64bit console.Coleorhiza
M
4

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>";
Marmawke answered 16/7, 2019 at 8:37 Comment(0)
L
2

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.

Leslie answered 9/1, 2012 at 21:21 Comment(1)
It's usually good form to link to the source (site) as well, assuming it's !NSFW.Nomi
L
2

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

powershell.exe -executionPolicy Unrestricted

Then it will surely work.

Levan answered 20/3, 2012 at 6:14 Comment(0)
N
-1

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

Newmann answered 29/2 at 17:47 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Kronstadt

© 2022 - 2024 — McMap. All rights reserved.