Pipeline返回True / False而不是匹配行

时间:2016-07-15 09:39:10

标签: powershell

我正在将源文件中的行复制到符合今天条目的日期模式(DD Monthname YYYY)的新文件中。正在使用True / False而不是实际行值将行复制到新文件。

$date_pattern = Get-Date -UFormat "%d %b %Y"
echo $date_pattern

(Get-Content $path\import.log) | foreach { $_ -match "$date_pattern" } |
    Set-Content $path\provider_import_report_$Today.log

源文件(import.log)如下所示:

ERROR 08 Apr 2016 20:36:28,904 [org.mule.api.processor.LoggerMessageProcessor] ([CMCHub].connector.file.mule.default.receiver.82:) ERROR in providers feed, UNSUCESSFUL upload response for id 'BARNS'
ERROR 08 Apr 2016 20:36:36,464 [org.mule.api.processor.LoggerMessageProcessor] ([CMCHub].connector.file.mule.default.receiver.79:) ERROR in providers feed, validation failed on file 'CF_output_edited.xml_000448.xml'ERROR 08 Apr 2016 20:36:44,120 [org.mule.api.processor.LoggerMessageProcessor] ([CMCHub].connector.file.mule.default.receiver.82:) ERROR in providers feed, UNSUCESSFUL upload response for id 'RYLAVIEW'
ERROR 08 Apr 2016 20:36:48,198 [org.mule.api.processor.LoggerMessageProcessor] ([CMCHub].connector.file.mule.default.receiver.82:) ERROR in providers feed, UNSUCESSFUL upload response for id 'A&ACARES'
ERROR 08 Apr 2016 20:36:55,432 [org.mule.api.processor.LoggerMessageProcessor] ([CMCHub].connector.file.mule.default.receiver.79:) ERROR in providers feed, provider record for Id 'ATHOMECA'
ERROR 08 Apr 2016 20:37:12,277 [org.mule.api.processor.LoggerMessageProcessor] ([CMCHub].connector.file.mule.default.receiver.82:) ERROR in providers feed, UNSUCESSFUL upload response for id 'ST.PETER'

新输出文件只包含true / false行。

True
True
True
False
False
False

1 个答案:

答案 0 :(得分:2)

foreach替换为Where-Object(或别名?),它将起作用 原因是在foreach块中返回布尔值,因为这是-match运算符的输出类型。

 ... | ? { $_ -match "$date_pattern" } | ...