如果我跑:
$output = shell_exec('powershell "get-service "dhcp""');
我得到了显示正在运行的服务dhcp的完美输出,但如果我运行:
$output = shell_exec('powershell "get-user "testing""');
我一无所获。
我没有看到我在这里做什么有什么不同 - 以及为什么get-service会起作用但是get-user不会。如果我在cmd中运行它可以很好地工作。有什么想法吗?
我认为问题可能是apache正在运行命令而没有权限。可能是这种情况吗? apache是以不同的用户身份运行的吗?如果是这样,则无权执行此操作。
答案 0 :(得分:2)
尝试将错误输出重定向到标准输出,以查看是否可以看到错误。
$output = shell_exec('powershell "get-user "testing" 2>&1"');
此代码段取自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");
答案 1 :(得分:0)
exec()和shell_exec()本质上不是很冗长。 exec()允许你设置第三个变量并获得执行状态,但失败主要是分配给#34; 1"如果二进制文件不可执行等,你无法知道它是否是权限错误。
输入一个项目,允许PHP使用真正的Powershell动态获取和交互。在此处获取:https://github.com/merlinthemagic/MTS
下载后,您只需使用以下代码:
$shellObj = \MTS\Factories::getDevices()->getLocalHost()->getShell('powershell');
$strCmd1 = 'get-user "testing"';
$return1 = $shellObj->exeCmd($strCmd1);
返回将为您提供来自powershell的命令return OR error,就像您坐在控制台一样。此外,您可以针对$ shellObj发出任何您喜欢的命令,在PHP脚本的整个生命周期中都会维护该环境。因此,不是在脚本文件中捆绑命令,而是使用exeCmd()方法逐个发出命令,这样您也可以处理返回和任何异常。