php - 通过 PHP 使用 Exchange 管理单元执行 powershell 脚本

标签 php apache powershell windows-server-2008 exchange-server

我正在尝试使用 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/

相关文章:

php - Laravel:如何打开多个域(不是子域)以显示来自同一服务器的页面(无重定向)?

function - 从写入 STDOUT 的函数返回值的最佳方法

php - 为什么 laravel 5.4 没有 see() 方法

php - 在 Codeigniter 的 View / Controller 中使用模型

php - 如何修复PHP中的 “Headers already sent”错误

java - 是否可以使用 Javascript/PHP 将文本渲染到 2D 图像上?

apache - 在目标中使用带有 HTTP_HOST 的 RedirectMatch

ruby - ubuntu 10.04 上的 phusion 乘客 : why apache2-prefork-dev when we want apache2-mpm-worker?

powershell - 使用powershell将VM名称保存到阵列

powershell - 使用参数但在 powershell 中保存 $args