Tail and parse log file

Ask your Windows 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.
Locked
User avatar
sekou2331
Posts: 262
Joined: Wed Aug 15, 2012 6:07 am

Tail and parse log file

Post by sekou2331 » Mon Apr 09, 2018 6:46 am

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()

User avatar
jvierra
Posts: 12874
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Tail and parse log file

Post by jvierra » Mon Apr 09, 2018 7:31 am

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

Locked