如何pipe理批处理脚本输出到PowerShell脚本?

我需要使用HTTP Apachepipe道日志logging将响应代码“400”的所有条目放到单独的文件中。 为此,我想使用PowerShell Select-String因为它是Linux中最接近grep命令:

我有以下批处理从cmd环境中调用PowerShell脚本:

 powershell -command "& { sls ,400, 'cmd -ca | select -exp line >> access_400.log }" 

据我所知,httpd中的“CustomLog”类似于这个:

 CustomLog "| 'call_powershell_script_that_will_extract_400_entries.ps1' " common 

如何将CustomLog的pipe道输出input到将使用“400”响应代码提取条目的PowerShell脚本?

在脚本中使用自动PowerShell变量$input

我不完全理解你的脚本,但你应该能够推断这个相似的例子:

filter.bat

 @powershell -command "$input | select-string -casesensitive -pattern '%1' | select-object -expandproperty line | out-file -append -encoding ASCII output.log" 

$input将填充BAT / CMD文件的控制台输入。

要从命令行测试,请使用dir | filter.bat ".exe" dir | filter.bat ".exe" ,然后type output.log

值得注意的是,我正在使用out-file而不是powershell重定向操作符,以便输出文件不会在powershell默认的UTF-16中编码,而是作为更常用的ASCII文件编码。