我正在尝试使用 PHP 执行的 powershell 脚本查询 Exchange 服务器,但它似乎不起作用。我尝试运行的命令是:
powershell "Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010";"Get-CASMailbox -Identity <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e693958394a6839e878b968a83c885898b" rel="noreferrer noopener nofollow">[email protected]</a> | fl ActiveSyncEnabled"
如果我直接在命令提示符中输入该内容,它会正确执行并返回:
ActiveSyncEnabled : True
当我在 PHP 中尝试时:
$output=shell_exec('powershell "Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010";"Get-CASMailbox -Identity <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="92e7e1f7e0d2f7eaf3ffe2fef7bcfde0f5" rel="noreferrer noopener nofollow">[email protected]</a> | fl ActiveSyncEnabled"');
我什么也没得到。 $output 已设置但为空。如果我在末尾添加“">output.txt”,我会得到一个空白文本文件。我可以通过 PHP 成功运行其他更简单的 powershell 命令,但不能运行这个命令。 Apache 服务以与我通过命令提示符成功执行脚本时相同的用户身份运行。
有什么想法吗?
我正在运行:
- Windows Server 2008 R2 SP1 64 位
- Apache 2.2.22 32 位
- PHP 5.2.17 VC6 32 位
- Exchange 管理控制台 2010
最佳答案
您可以尝试在命令末尾添加2>&1
,例如:
$output=shell_exec('powershell "Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010";"Get-CASMailbox -Identity <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4134322433012439202c312d246f2e3326" rel="noreferrer noopener nofollow">[email protected]</a> | fl ActiveSyncEnabled" 2>&1');
有时,powershell 输出开头的换行符似乎会阻止 shell_exec
打印其他任何内容。
关于php - 通过 PHP 使用 Exchange 管理单元执行 powershell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10143312/