如何管道批处理脚本输出到PowerShell脚本? [英] How to pipeline batch script output to a PowerShell script?

查看:367
本文介绍了如何管道批处理脚本输出到PowerShell脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用Apache的HTTP管道日志与响应code400的所有条目放到一个单独的文件。对于这一点,我想用PowerShell的选择字符串,因为它是最接近Linux中的的grep 命令:

I need to use HTTP Apache piped logging to place all entries with response code "400" into a separate file. For that, I want to use PowerShell Select-String since it is the closest thing to the grep command in Linux:

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

I have the following batch to call a PowerShell script from within a cmd environment:

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

据我所知,的CustomLog中的httpd将类似于这样:

As far as I know, the "CustomLog" in httpd will be similar to this:

CustomLog "| 'call_powershell_script_that_will_extract_400_entries.ps1' " common

我怎样才能的CustomLog的管道输出输入到PowerShell脚本,将与400响应code?

How can I make the piped output of CustomLog be input to the PowerShell script that will extract the entries with "400" response code?

推荐答案

使用自动的PowerShell变量, $输入,在您的脚本。

Use the automatic PowerShell variable, $input, in your script.

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

I don't totally understand your script, but you should be able to extrapolate this similar example:

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

$输入将充满了BAT / CMD文件中的控制台输入。

$input will be filled with the console input for the BAT/CMD file.

要测试,在命令行中使用 DIR | filter.bat名为.exe,那么键入output.log

To test, from the command line, use dir | filter.bat ".exe", then type output.log.

值得注意的是,我使用了文件,而不是PowerShell的重定向操作,使输出文件不带在PowerShell中默认的codeD,UTF-16,但作为一个更普遍使用的ASCII文件。

Notably, I'm using out-file instead of the powershell redirection operator so that the output file is not encoded in the powershell default, UTF-16, but as a more generally usable ASCII file.

这篇关于如何管道批处理脚本输出到PowerShell脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆