Proper way to Tail (Get-Content) a Rotating log File

Hello,

I am using the script below to post new entries from a file to an endpoint. The issue I am experiencing is that the file rotates every X days. This script is left running (probably should be a service or job) and will error out every X days after the rotation.

What is the proper way to handle this in PowerShell? Is there an option to force or retry similar to Linux tail?

$filename = "PathToSomeFile.csv"
$URI = "http://SomeAPI_Endpoint"
$headers = @{ Authorization = $basicAuthValue }

Get-Content -Path $filename -Tail 0 –Wait | ForEach-Object {Invoke-RestMethod -Uri $URI  -Headers $headers -Method 'Post' -Body @{"message"="$_"}}

-Thank you

There is a -Tail in PS 5

https://blogs.technet.microsoft.com/rmilne/2016/06/03/powershell-tail-command/

 

Thank you, Iain.

If you notice, I am already using that (-Tail 0) in the provided script.

What I am looking for are suggestions or script examples of how to deal with tailing a log file that rotates.
-Thank you

My apologies, my brain stopped at “…similar to Linux tail?”

If the log file name remains the same, as it does with Sql Server for example, it should be read ok.

What version of PowerShell are you using ?

 

In this situation PSVersion is 4.0.
On Sundays at midnight the file is renamed and a new file created with the same file name as before (the file I am tailing).
This seems to be causing the error.

I just tested with PS 5.1 and it works when I read a Sql Server log and cycled it to use a new log with the same name…

$filename="C:\Program Files\Microsoft SQL Server\MSSQL14.DEV\MSSQL\Log\ERRORLOG"
Get-Content-Path $filename-Tail 1 –Wait

If your log changes its name each roll then you need to plan your code for that. Set you code to look for the latest log name by timestamp.

Thank you both for your help.

It appears I was able to get around this using a do loop and a small delay/retry.

Start-Transcript -Path "C:\Log-$dt.txt"

do{
    try{
        Get-Content -Path $filename -Tail 0 –Wait -ErrorAction Stop| ForEach-Object {Invoke-RestMethod -Uri $URI -Headers $headers -Method 'Post' -Body @{"message"="$_"}; Write-Host "$(timestamp)Msg: $_" -NoNewline; } 
        $success = $true
    }
    catch{
        Write-Output "File Not Found"
        Start-sleep -Seconds 1
    }
    
    $count++
}until($count -eq 5 -or $success)

if(-not($success)){Stop-Transcript | exit}