I have a perl script that is used to monitor databases and I'm trying to write it as a powershell script.
In the perl script there is a function that reads through the errorlog and filters out what it is that matters and returns it back. It also saves the current position of the log file so that the next time it has to read the log it can start where it left of instead of reading the whole log again. This is done by using the tell function.
I have an idea to use the Get-Content cmdlet, and start reading at the last position, process each line until the end of the file and then saving the position.
Do you know any tricks so that I can get the position in the log file after reading and make the read start at a particular location.
Or is there an better and/or easier way to achieve this?
Gísli
EDIT: This has to be done through the script and not with some other tool.
EDIT: So I'm getting somewhere with the .NET API but it's not quite working for me. I found a helpful links here and here
Here is what I have so far:
function check_logs{
param($logs, $logpos)
$count = 1
$path = $logs.file
$br = 0
$reader = New-Object System.IO.StreamReader("$path")
$reader.DiscardBufferedData()
$reader.BaseStream.Seek(5270, [System.IO.SeekOrigin]::Begin)
for(;;){
$line = $reader.ReadLine()
if($line -ne $null){$br = $br + [System.Text.Encoding]::UTF8.GetByteCount($line)}
if($line -eq $null -and $count -eq 0){break}
if($line -eq $null){$count = 0}
elseif($line.Contains('Error:')){
$l = $line.split(',')
Write-Host "$line $br"
}
}
}
I haven't found a way to use the seek function correctly. Can someone point me in the right direction?
If I run this it outputs 5270 but if I run this with out the line where I try to seek in the base stream I get:
2011-08-12 08:49:36.51 Logon Error: 18456, Severity: 14, State: 38. 5029
2011-08-12 08:49:37.30 Logon Error: 18456, Severity: 14, State: 38. 5270
2011-08-12 16:11:46.58 spid18s Error: 1474, Severity: 16, State: 1. 7342
2011-08-12 16:11:46.68 spid18s Error: 17054, Severity: 16, State: 1. 7634
2011-08-12 16:11:46.69 spid29s Error: 1474, Severity: 16, State: 1. 7894
Where the first part is the line read from the log and the number at the end represents the bytes read at that point. So as you can see I'm now trying to use the seek function to skip the first error line but as I said earlier the output is 5270 if I use the seek function.
What am I missing?????
Gísli