PowerShell 管道进入 find.exe 命令

Vic*_*Vic 10 powershell

只是好奇,为什么会发生这种情况?如果我运行:

netstat -an | find "443"

进入命令提示符,“443”连接显示正常。如果我在 PowerShell 控制台或 ISE 中运行相同的命令,则会收到错误“FIND:参数格式不正确”。netstat 输出是否没有通过管道正确传输以在 PS 中查找?

注意:如果我运行netstat -an | findstr "443"netstat -an | select-string "443"在 PS 中,它们会按预期工作。

jsc*_*ott 17

PowerShell 评估双引号内的内容以执行任何变量扩展、子表达式等,然后丢弃这些双引号。PowerShell 返回的"443"是字面意思443(注意缺少的引号)。FIND.EXE 需要用双引号括起来的搜索字符串。

如果您想阻止 PowerShell 剥离双引号,请使用重音符号 (`) 来转义它们。

netstat -a -n  | find `"443`"
Run Code Online (Sandbox Code Playgroud)

您也可以使用该--%参数来执行转义。需要 PowerShell 3+。

nestat -a -n | find --% "443"
Run Code Online (Sandbox Code Playgroud)

  • @Vic `findstr` 实用程序不需要`/C` 字符串参数中的双引号:`findstr /C:somestring somefile` 与`findstr /C:"somestring" somefile` 一样工作。对于`FIND`,需要双引号。 (3认同)