PHP exec,仅输出错误

PHP exec,仅输出错误,php,Php,我正在使用PHPExec运行一个Powershell脚本来创建一个通过HTML表单提交的新用户。我要做的是在网页上只输出这个命令中的错误,这样任何IT团队成员都可以看到是否有任何错误。以下是我所拥有的: $psPath = 'c:\\Windows\\System32\WindowsPowerShell\v1.0\\powershell.exe -version 5'; $psDIR = "d:\\wamp64\\www\\includes\\"; $psScript = "NewHire.ps

我正在使用PHPExec运行一个Powershell脚本来创建一个通过HTML表单提交的新用户。我要做的是在网页上只输出这个命令中的错误,这样任何IT团队成员都可以看到是否有任何错误。以下是我所拥有的:

$psPath = 'c:\\Windows\\System32\WindowsPowerShell\v1.0\\powershell.exe -version 5';
$psDIR = "d:\\wamp64\\www\\includes\\";
$psScript = "NewHire.ps1";
$runCMD = $psPath. ' -ExecutionPolicy RemoteSigned '.$psDIR.$psScript

$createuser = exec($runCMD.' 2>&1', $out);
当我执行
var\u dump($out)
时,它同时显示输出和错误。我尝试将其更改为
exec($runCMD.&1',$out)
,但它只显示输出。如果我尝试执行($runCMD'2>,$out),它不会运行我的命令。有没有办法只在$out变量中显示错误?

试试这个

exec($runCMD."2>&1 1>/dev/null ",$out);
试试这个

exec($runCMD."2>&1 1>/dev/null ",$out);

发现这只适用于stderr:

exec($runCMD.' >&2' , $out);

发现这只适用于stderr:

exec($runCMD.' >&2' , $out);
也许这会有帮助

public static function exec_alt($cmd)
{
    exec($cmd, $output);
    if (!$output) {
        /**
         * FIXME: for some reason exec() returns empty output array @mine,'s machine.
         *        Somehow proc_open() approach (below) works, but doesn't work at
         *        test machines - same empty output with both pipes and temporary
         *        files (not we bypass shell wrapper). So use it as a fallback.
         */
        $output = array();
        $handle = proc_open($cmd, array(1 => array('pipe', 'w')), $pipes, null, null, array('bypass_shell' => true));
        if (is_resource($handle)) {
            $output = explode("\n", stream_get_contents($pipes[1]));
            fclose($pipes[1]);
            proc_close($handle);
        }
    }
    return $output;
}
如果您使用windows 7,您可能需要:

禁用用户帐户控制(UAC)

UserAccountControlSettings.exe

C:\Windows\System32\UserAccountControlSettings.exe
然后选择“从不通知”

也许这会有所帮助

public static function exec_alt($cmd)
{
    exec($cmd, $output);
    if (!$output) {
        /**
         * FIXME: for some reason exec() returns empty output array @mine,'s machine.
         *        Somehow proc_open() approach (below) works, but doesn't work at
         *        test machines - same empty output with both pipes and temporary
         *        files (not we bypass shell wrapper). So use it as a fallback.
         */
        $output = array();
        $handle = proc_open($cmd, array(1 => array('pipe', 'w')), $pipes, null, null, array('bypass_shell' => true));
        if (is_resource($handle)) {
            $output = explode("\n", stream_get_contents($pipes[1]));
            fclose($pipes[1]);
            proc_close($handle);
        }
    }
    return $output;
}
如果您使用windows 7,您可能需要:

禁用用户帐户控制(UAC)

UserAccountControlSettings.exe

C:\Windows\System32\UserAccountControlSettings.exe

然后选择“从不通知”

@codedByMi我已经执行了我的代码,它工作正常。我试过你的例子,但是我的命令没有运行。我应该提到这是一个WAMP服务器。@codedByMi我已经执行了我的代码,它工作正常。我试过你的例子,但是我的命令没有运行。我应该提到这是一个WAMP服务器。