2016-05-16 70 views
1

我想要來自文件夾中所有文件的錯誤消息,並僅將每個文件的錯誤消息打印到新的txt文檔。這是我迄今爲止所做的。我不知道如何繼續將錯誤信息保存到新的txt文檔。Powershell獲取內容和Get-ChildItem

我只希望所有日誌文件中的每一行都將該錯誤消息保存到新文檔中。因此,當您打開新文檔時,您只能看到錯誤信息和來自哪裏的路徑。

Get-ChildItem "H:\Script\Log_test" -Filter "*.log" -recurse | Select-String -pattern "error" | group path | select name | Out-File "H:\Script\Log_test\result.txt" 

回答

1

只是selectSelect-String cmdlet返回與-expand開關MatchInfo對象的Line屬性:

Get-ChildItem "H:\Script\Log_test" -Filter "*.log" -recurse | 
    Select-String -pattern "error" | 
    select -expand Line | 
    Out-File "H:\Script\Log_test\result.txt" 
+0

謝謝!在過濾文件的同時,是否可以過濾文件夾? – Thun

+0

你究竟是什麼意思? –

+0

當我使用-Filter「* .log」我可以使用這個文件夾嗎? – Thun