Tail and parse log file

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 6 years and 1 week old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Tail and parse log file

Post by sekou2331 »

Hi,

I am trying to parse while tailing a log file and output it to another file. I tried both of the codes below. For the first script it seems to be not working. it looks like it is stopping the log file. As for the second it is not checking continuously. Is there a better way of doing this or am I just not writing this correctly.


Code: Select all

$tail_log = Get-Content 'C:\TestLogging.log' -wait
while($tail_log){
sls -Path $tail_log -Pattern 'Added sample 08', 'Added sample 11', 'Added sample 10', 'Added sample 20' | Out-File .\Outlogfile.log -Append
}

Code: Select all

[System.IO.FileStream]$fileStream = [System.IO.File]::Open('C:\TestLoggging.log', [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read, [System.IO.FileShare]::ReadWrite)
$byteArray = New-Object byte[] $fileStream.Length
$encoding = New-Object System.Text.UTF8Encoding $true
while ($fileStream.Read($byteArray, 0, $byteArray.Length)) {
	$encoding.GetString($byteArray) | Out-File  .\Outlogfile.log
	(sls -path ".\Outlogfile.txt" -Pattern '23', '100', '10', '20').line
}
$fileStream.Dispose()
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Tail and parse log file

Post by jvierra »

You are not tailing at all. Here is how to tail a file:

Code: Select all

Get-Content C:\TestLogging.log -wait -Tail 1 |
    Select-String -Pattern 'Added sample 08|Added sample 11|Added sample 10|Added sample 20' | 
    Out-File .\Outlogfile.log
This topic is 6 years and 1 week old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked