I know that is not a good idea runs a script for ever but, I need to continuously tail a log file, for some days, to get the evidence of some errors.
Problem is that this log file rotates multiple times during the day and I don't know how to follow it with powershell...
I'm stuck here:
$SRV = hostname
$FPath = "C:\Logs"
$LastFile = ( Get-Item "$FPath\*" | Where-Object {$_.Name -match "Module"} | sort LastWriteTime | select -last 1 ).FullName
$LastActiveFile = (Get-FileHash -Algorithm MD5 $LastFile).Hash
$ScriptPath = "E:\Scripts\ReInvite"
$CredPath = "$ScriptPath\Creds"
$logFile = "$ScriptPath\Logs\EventsReinvites.txt"
$CallEvent = "(^[\s\S]{0,21})(.*?)(CALLID:(.*?)@10)"
While ( (Get-FileHash -Algorithm MD5 -Path $(( Get-Item "$FPath\*" | Where-Object {$_.Name -match "Module.log"} | sort LastWriteTime | select -last 1 ).FullName)).Hash -eq $LastActiveFile ) {
Get-Content "$LastFile" -Tail 1 -Wait | foreach {
if ( $_ -match "REINVITE" ) {
$SipEvent = [regex]::match($_, $CallEvent).Groups[1,4].Value
$SipEvent | Out-File -Append $logFile
$Subject = "RMOD -> REINVITE event triggered..."
$MSG = "Evento 'REINVITE' ricevuto su $SRV:"
$MSG1 = "$SipEvent"
$MSG2 = " "
$MSG3 = "Script Path: \\$env:COMPUTERNAME\$($ScriptPath.replace(':', '$')) "
$MSG4 = " "
$body = "$MSG" + "`n`n" + "$MSG1" + "`n" + "$MSG2" + "`n`n`n" + "$MSG3" + "`n" + "$MSG4"
Send-MailMessage -Subject $Subject -Body $body @MailArgs | Out-File "$LogDir\MailLogs.txt"
}
}
}
Script do the job but, as soon as log rotates, an error occour:
Get-Content : Il file 'C:\Logs\Module.2024-07-09T07-28-27-355.1.log' non è stato trovato.
In riga:1 car:1
+ Get-Content "C:\Logs\Module.2024-07-09T07-28-27-355.1.log
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ReadError: (C:\Logs\Module...28-27-355.1.log:String) [Get-Content], FileNotFoundException
+ FullyQualifiedErrorId : GetContentReaderIOError,Microsoft.PowerShell.Commands.GetContentCommand
and script exits...
Please, help me! Roberto
FileNotFoundException