LineNumber not reporting correctly


hi everyone

i've been working on side project of mine helps me learn powershell , incorporate other languages, , i've hit bit of roadblock.

my powershell script reads latest entry (the last line) of logfile everytime logfile has been updated.  when logfile updates, script reads last line of logfile , searches 2 specific words; gamestart , gameend.

once finds 'gamestart' supposed remember linenumber , continue read latest entries until finds 'gameend' , remember linenumber.

problem i'm having no matter line position, gamestart report linenumber 0 , gameend relevant position of gamestart, , not actual linenumber. (so if gameend found 77 lines down, reports linenumber 77 not actual linenumber)

i'm using section of code monitor logfile updates:

http://dereknewton.com/2011/05/monitoring-file-system-changes-with-powershell/

for testing purposes, have below lines confirm if position of gamestart , gameend reports correct line number:

$changed = register-objectevent $watcher1 "changed" -action {
$gamestart = get-content 'c:\path-to-log\logfile.log' -tail 1 | select-string -pattern '(gamestart)'
$gameend = get-content 'c:\path-to-log\logfile.log' -tail 1 | select-string -pattern '(gameend)'
if ($gamestart)
{
write-host $(get-date -format t) "gamestart entry found on line "$gamestart.linenumber
}
else ($gameend)
{
write-host $(get-date -format t) "gamestart entry found on line "$gameend.linenumber
}
}
ideas gamestart , gameend report actual correct linenumber?

try

$gamestart = @(get-content 'c:\path-to-log\logfile.log' | select-string -pattern '(gamestart)')[-1] $gameend = @(get-content 'c:\path-to-log\logfile.log' | select-string -pattern '(gameend)')[-1] 



Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

directory stack

After enabling Windows Server 2012 R2 DHCP Failover Getting Packet dropped because of Client ID hash mismatch

WMI Repository 4GB limit - Win 2003 Ent Question