4

If I run:

$output = shell_exec('powershell "get-service "dhcp""'); 

I get perfect output of the service dhcp showing running but if I run:

$output = shell_exec('powershell "get-user "testing""'); 

I get nothing.

I don't see any difference in what Im doing here - and why get-service would work but get-user would not. If I run it in cmd it works perfectly. Any ideas?

I believe the issues might be that apache is running the command and does not have permissions. Could this be the case? Does apache run as a different user? If so it doesn't have access to do this.

5
  • Wasn't that question posted a few hours ago already? Commented Mar 2, 2010 at 1:03
  • @Franz - is this the post you were referring to? stackoverflow.com/questions/2353551/php-powershell-command Commented Mar 2, 2010 at 1:22
  • 1
    Get-User is not a built-in command. Can you say a little about this cmdlet like which module or snapin it comes from? Or is it a command that you wrote? BTW, I would run this from cmd.exe like so powershell -command "& {get-user testing}". Note that is normally unnecessary to quote string arguments in PowerShell. Commented Mar 2, 2010 at 1:23
  • The question before was about the quotes issue I had when using shell_exec. This one is a permissions issue I believe but want to clarify. Thanks. Commented Mar 2, 2010 at 1:35
  • Sorry. Should have checked this ;) Commented Mar 2, 2010 at 8:53

2 Answers 2

3

Try redirecting the error output to the standard output to see if you can see an error.

$output = shell_exec('powershell "get-user "testing" 2>&1"'); 

This snippet taken from http://www.aboutdebian.com/nettools.txt

//Normally, the shell_exec function does not report STDERR messages. 
//The   "2>&1"          option tells the system 
//to pipe STDERR to STDOUT so if there is an error, we can see it.
$fp = shell_exec("$command 2>&1");
Sign up to request clarification or add additional context in comments.

Comments

1

exec() and shell_exec() are not very verbose by nature. exec() allows you to set a third variable and get the execution status, but failures are mostly assigned "1" and you have no way of knowing if it was a permissions error, if the binary is not executable etc.

Enter a project that allows PHP to obtain and interact dynamically with a real Powershell. Get it here: https://github.com/merlinthemagic/MTS

After downloading you would simply use the following code:

$shellObj    = \MTS\Factories::getDevices()->getLocalHost()->getShell('powershell');

$strCmd1   = 'get-user "testing"';
$return1  = $shellObj->exeCmd($strCmd1);

The return will give you the command return OR error from powershell, just as if you sat at the console. Furthermore, you can issue any command you like against the $shellObj, the environment is maintained throughout the life of the PHP script. So instead of bundling commands in a script file, just issue them one by one using the exeCmd() method, that way you can also handle the return and any exceptions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.