Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Powershell command

Tags:

php

powershell

Trying to run the following command in php to run powershell command...

the following works:

$output = shell_exec(escapeshellcmd('powershell get-service | group-object'));

I can not run it like this:

$output = shell_exec('powershell get-service | group-object');

it will not pass the pipe | character

but if I try to run:

$output = shell_exec(escapeshellcmd('powershell get-service | where-object {$_.status -eq "Running"}'));

I get no output.

The following:

$cmd = escapeshellcmd('powershell get-service | where-object {$_.status -eq "Running"}');

returns:

powershell get-service ^| where-object ^{^$_.status -eq ^"Running^"^}

Any suggestions on why this is happening and how to fix this?

Edit: Also I could run it as .ps1 script but I want to be able to pass $var to it.

like image 203
Michael Burns Avatar asked Mar 01 '10 02:03

Michael Burns


People also ask

How do I run PHP in PowerShell?

Open powershell and type c:\php\php.exe -h , you will get the php help output. Yay you are up and running, whoot. Type env into os search (cortana) and select environmental variables. Now you can run php in powershell with php ( php -h to test).

What does $() mean in PowerShell?

Subexpression operator $( ) For a single result, returns a scalar. For multiple results, returns an array. Use this when you want to use an expression within another expression. For example, to embed the results of command in a string expression. PowerShell Copy.

What does the command @() mean in PowerShell?

One more thing... an empty array (like to initialize a variable) is denoted @() .


2 Answers

I'll take a stab although I have no PHP experience whatsoever.

I have a feeling that what's happening is your pipe character is being interpreted by the command shell instead of PowerShell. For example if you ran the following at the cmd.exe command prompt:

dir /s | more

The output of the first command gets piped to the input of the second just like you'd expect in PowerShell.

Escaping the string will only make the problem worse because you're transforming the string in such a way that PowerShell has no idea how to unescape it.

Try enclosing your original PowerShell expression in a quote like the following:

$output = shell_exec('powershell.exe -c "get-service | group-object"');

Or preferably, it looks like there's an exec() function that does not go through the command shell. This might work better.

$output = exec('powershell.exe -c get-service | group-object');
like image 68
Josh Avatar answered Oct 08 '22 12:10

Josh


'powershell get-service | group-object'

will be interpreted as

  1. run powershell and pass it get-service as an argument
  2. then pipe the output of powershell to group_object (i.e. not the output of get-service)

What you want is for powershell to see get-service | group-object as it's argument, so you have to enclose that in quotes, like this.

 $output = shell_exec('powershell "get-service | group-object"');
like image 41
John Knoeller Avatar answered Oct 08 '22 11:10

John Knoeller