PHP将列表从Powershell放入数组

PHP将列表从Powershell放入数组,php,list,powershell,exchange-server,Php,List,Powershell,Exchange Server,代码: 我试过了 echo( $output[1] ); 结果只有一个字母“N”。我相信它一次只取一个字符 $output[1] is 'N', $output[2] is 'a'. 有什么方法可以将邮箱列表放入数组中吗?您正试图从PHP执行一个外部程序(powershell),并将输出作为数组。 为了在PHP中执行外部程序,您可以使用: 作用 作用 作用 使用过程控制扩展(PCNTL、popen)可以提供更多的控制,但需要更多的代码和时间。使用执行函数更简单 在这种情况下,使用ex

代码:

我试过了

echo( $output[1] );
结果只有一个字母“N”。我相信它一次只取一个字符

$output[1] is 'N', $output[2] is 'a'.

有什么方法可以将邮箱列表放入数组中吗?

您正试图从PHP执行一个外部程序(powershell),并将输出作为数组。 为了在PHP中执行外部程序,您可以使用:

  • 作用
  • 作用
  • 作用
使用过程控制扩展(PCNTL、popen)可以提供更多的控制,但需要更多的代码和时间。使用执行函数更简单

在这种情况下,使用exec()可以帮助您将powershell的输出放在一个数组中,该数组的每个索引都是powershell输出的一行

<?php
$output = array(); // this would hold the powershell output lines
$return_code = 0; // this would hold the return code from powershell, might be used to detect execution errors
$last_line = exec("powershell {$exchangesnapin} get-mailboxdatabase 2>&1", $output, $return_code);
echo "<pre>";
// print_r($output); view the whole array for debugging
// or iterate over array indexes
foreach($output as $line) {
    echo $line . PHP_EOL;
}
echo "</pre>";
?>

请注意(如文档所述),如果您只想回显powershell的输出,则可以使用该函数。使用exec()使用内存存储外部程序的输出,但使用passthru不会使用此存储,从而减少内存使用。但是输出不能用于进一步的处理,并且以正确的方式发送到PHP标准输出


最后,请注意,外部程序执行需要仔细的数据验证,以降低不必要的系统影响的风险。确保对构造执行命令的数据使用。PHP不是将PowerShell输出的所有内容都转换为字符串吗?您需要使用文本解析技术将其转换为PHP中的数组。使用该解决方案,我可以将其放入下拉菜单中。非常感谢你!
$output[1] is 'N', $output[2] is 'a'.
<?php
$output = array(); // this would hold the powershell output lines
$return_code = 0; // this would hold the return code from powershell, might be used to detect execution errors
$last_line = exec("powershell {$exchangesnapin} get-mailboxdatabase 2>&1", $output, $return_code);
echo "<pre>";
// print_r($output); view the whole array for debugging
// or iterate over array indexes
foreach($output as $line) {
    echo $line . PHP_EOL;
}
echo "</pre>";
?>