2

I'm attempting to query an Exchange server using a powershell script executed by PHP and it doesn't seem to be working. The command I'm attempting to run is:

powershell "Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010";"Get-CASMailbox -Identity [email protected] | fl ActiveSyncEnabled"

If I type that directly into the command prompt, it executes correctly and returns:

ActiveSyncEnabled : True

When I try it in PHP:

$output=shell_exec('powershell "Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010";"Get-CASMailbox -Identity [email protected] | fl ActiveSyncEnabled"');

I get nothing. $output is set but blank. If I add "> output.txt" to the end, I get a blank text file. I'm able to run other, simpler powershell commands successfully via PHP, but not this one. The Apache service is running as the same user as when I successfully executed the script via the command prompt.

Any ideas?

I'm running:

  • Windows Server 2008 R2 SP1 64-bit
  • Apache 2.2.22 32-bit
  • PHP 5.2.17 VC6 32-bit
  • Exchange Management Console 2010
1
  • For testing purposes, I set the Apache service to run as myself. In PHP, exec('whoami') returns my domain\username, the same username I used when I successfully executed the command at the command prompt. Shouldn't this account for any permissions issues, or am I missing something? Commented Apr 13, 2012 at 15:16

1 Answer 1

2

You may try adding 2>&1 at the end of the command, e.g.:

$output=shell_exec('powershell "Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010";"Get-CASMailbox -Identity [email protected] | fl ActiveSyncEnabled" 2>&1');

Sometimes, a line break at the beginning of the powershell output seems to prevent shell_exec printing anything else.

Sign up to request clarification or add additional context in comments.

10 Comments

Thank you for your assistance! It still doesn't work, but at least now I'm getting an error: Add-PSSnapin : No snap-ins have been registered for Windows PowerShell version 2. At line:1 char:13 + Add-PSSnapin <<<< Microsoft.Exchange.Management.PowerShell.E2010;Get-CASMail box -Identity [email protected] | fl ActiveSyncEnabled + CategoryInfo : InvalidArgument: (Microsoft.Excha...owerShell.E2 010:String) [Add-PSSnapin], PSArgumentException + FullyQualifiedErrorId : AddPSSnapInRead,Microsoft.PowerShell.Commands.Ad dPSSnapinCommand
I edited my previous comment with the error I'm getting now. I also tried entering Shedal's string directly into the command prompt, and it still executes correctly, returning AciveSyncEnabled : True
@amay83 The error may be because it's the wrong powershell executable you're using. Refer to this article: blogs.msdn.com/b/pasen/archive/2011/04/06/…
It seems, the problem is because the snap-in is compiled for x64 while your shell runs in a 32-bit mode. So it doesn't see the snap-in. I found a comment which suggests how you can run the shell in another mode: php.net/manual/en/function.shell-exec.php#84992. Maybe it's vice-versa, though, and you need it in 32-bit mode while you run in 64-bit mode.
That didn't work, but thank you very much for identifying the problem! I ended up installing Apache x64 and PHP x64, which I got from here: anindya.com. That solved the issue, and now all the 64-bit apps are playing nicely together.
|

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.