PHP Powershell command

Question:

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

the following works:

I can not run it like this:

it will not pass the pipe | character

but if I try to run:

I get no output.

The following:

returns:

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.

Answer:

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:

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:


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

Source:

PHP Powershell command by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply