Last active
February 10, 2016 08:27
-
-
Save midnightfreddie/5870bda5e4d50f73f61b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Refactored from https://www.reddit.com/r/PowerShell/comments/44zkff/simple_script_for_looking_through_text_files_for/ | |
# Lots of comments explaining changes removed. Look at the revision history in Gist or the commit messages in git for explanations. | |
param( | |
[string] $LogDirectory = "C:\Projects\Patterns\Reports\Run01\logs", | |
[string] $ResultFileDate = ("Results_{0}.csv" -f (MyDate)), | |
[string] $ExtractedFilePath = "$($LogDirectory.TrimEnd('\'))\$($ResultFileDate)", | |
[Array] $patterns= @( | |
',vsftp ,Connection closed.' | |
'The sftp operation was aborted.' | |
) | |
) | |
function MyDate { Get-Date -Format "MM-dd-yyyy-hh-mm-ss" } | |
Get-ChildItem $LogDirectory -Filter *.log | | |
ForEach-Object { | |
$File = $_ | |
Write-Host ("{0}: File being processed is {1}`n" -f (MyDate), $File.Name) | |
$File.FullName | |
$patterns | ForEach-Object { | |
$pattern = $_ | |
Get-Content $File | | |
select-string -pattern $pattern | | |
ForEach-Object { | |
$_ -replace "time: ", "time," ` | |
-replace " seconds.", "" ` | |
-replace " secs.", "" | |
} | |
} | |
} | | |
Add-Content $ExtractedFilePath | |
Write-Host ("{0}`nResults available in {1}`n" -f (MyDate), $ExtractedFilePath) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment