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

Welcome Forums General PowerShell Q&A Proper way to Tail (Get-Content) a Rotating log File

This topic contains 7 replies, has 3 voices, and was last updated by

 
Participant
2 months, 1 week ago.

  • Author
    Posts
  • #165583

    Participant
    Topics: 1
    Replies: 3
    Points: -2
    Rank: Member

    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

  • #165619

    Participant
    Topics: 37
    Replies: 155
    Points: 479
    Helping Hand
    Rank: Contributor
  • #165628

    Participant
    Topics: 1
    Replies: 3
    Points: -2
    Rank: Member

    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

  • #165637

    Participant
    Topics: 37
    Replies: 155
    Points: 479
    Helping Hand
    Rank: Contributor

    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 ?

     

  • #165643

    Participant
    Topics: 1
    Replies: 3
    Points: -2
    Rank: Member

    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.

  • #165646

    Participant
    Topics: 37
    Replies: 155
    Points: 479
    Helping Hand
    Rank: Contributor

    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
  • #165682

    Participant
    Topics: 2
    Replies: 999
    Points: 1,946
    Helping Hand
    Rank: Community Hero

    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.

  • #166087

    Participant
    Topics: 1
    Replies: 3
    Points: -2
    Rank: Member

    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}
    

You must be logged in to reply to this topic.