带有管道符号和变量的PHP powershell cmdlet问题

带有管道符号和变量的PHP powershell cmdlet问题,php,powershell,pipe,shell-exec,cmdlet,Php,Powershell,Pipe,Shell Exec,Cmdlet,我寻找这个问题的答案,找到了这篇文章 但是,当插入一些php变量时,我无法使建议的工作正常,我传递的命令是: $command = "powershell -command get-aduser -Filter {(givenname -Like '*".$username[0]."*') -And (surname -Like '*".$username[1]."*')} -Properties samaccountname | format-table samaccountname 2&g

我寻找这个问题的答案,找到了这篇文章

但是,当插入一些php变量时,我无法使建议的工作正常,我传递的命令是:

 $command = "powershell -command get-aduser -Filter {(givenname -Like '*".$username[0]."*') -And (surname -Like '*".$username[1]."*')} -Properties samaccountname | format-table samaccountname 2>&1";
我得到了一个错误 字符串“format-table”未被识别为内部或外部命令, 可操作的程序或批处理文件。 '(长度=101)


如何使其工作,以便它使用变量并让我将其传送到表中?

要测试这一点,您应该将该命令粘贴到windows命令提示符中

管道
|
在到达powershell之前就被命令解析器解释,因此管道之后的任何内容都被视为单独的可执行文件或要运行的命令

您可以尝试引用整个命令字符串,但也可能需要费尽周折才能避开任何嵌入的双引号

但最简单的方法可能是直接转义管道。windows命令提示符的转义字符是插入符号
^

$command = "powershell -command get-aduser -Filter {(givenname -Like '*".$username[0]."*') -And (surname -Like '*".$username[1]."*')} -Properties samaccountname ^| format-table samaccountname 2>&1";

修好了!非常感谢,我整个上午都在搞砸它。