Executing a powershell script with Exchange Snap-In via PHP

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 user@example.com | 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 user@example.org | 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


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 user@example.org | fl ActiveSyncEnabled" 2>&1');

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

Need Your Help

Saving user settings/database/cache... in Java (On every OS)

java operating-system settings configuration-files

My Java application is saving stuff in 'user.home' but on windows this does not seem to be the correct path to save application information (as a friend told me). An other option is using the prefe...