Home > other >  Powershell read file for text wait until found then proceed
Powershell read file for text wait until found then proceed

Time:10-16

I'm working on a process to start up some services across remote servers, however server 2 can't start up until a message is found in server1 logs, server 3 can't start until same message in server 2, etc.

My question is, is it possible to read the file in a "loop" and not proceed with my initial loop until that message is found (and then move forward)? I was thinking I could do below, however while it does recognize that the string in the log file is found, it just repeats that it found it until the timer built in finishes and then moves forward. So, the process would look like "read this file, if string is found, move forward. If string is not found, wait 30 seconds and rescan the file. If found, move forward, if not found, wait an additional 30 seconds and rescan (I'm fine with a continuous repeat) < do this until string is found. Example: enter image description here

Any advice would be much appreciated as I think I might be approaching this from the wrong angle...

-- I left out the majority of the script prior to this script and only included the If/Else statement as this is where it checks the files.

$SEL = Select-String -Path \\$Server\$RootDir\folder\anotherfolder\A-Log-File.log -Pattern "Switching to status: STARTED"
if ($SEL -ne $null)
{
    Write-Host "FOUND: Switching to status: STARTED" -ForegroundColor Yellow -BackgroundColor DarkGreen
}
else
    {
        Write-Host **** Waiting 60 seconds for cache to build ****
            [int]$Time = 60
            $Lenght = $Time / 100
            For ($Time; $Time -gt 0; $Time--) {
            $min = [int](([string]($Time/60)).split('.')[0])
            $text = " "   $min   " minutes "   ($Time % 60)   " seconds left"
            Write-Progress -Activity "Waiting for Started Message" -Status $Text -PercentComplete ($Time / $Lenght)
            Start-Sleep 1
        $SEL = Select-String -Path \\$Server\$RootDir\folder\anotherfolder\A-Log-File.log -Pattern "Switching to status: STARTED"
        if ($SEL -ne $null)
        {
            Write-Host "FOUND: Switching to status: STARTED" -ForegroundColor Yellow -BackgroundColor DarkGreen
        }
        else
            {
                Write-Host **** A-Log-File.log Log does NOT contain a started message **** -ForegroundColor Red -BackgroundColor Yellow 
                Write-Host **** Investigate further or increase the int-time time on Line 54 to 180 seconds **** -ForegroundColor Red -BackgroundColor Yellow ##This part goes away once action can be taken based on reading contents of the file
            }
                                                }
    }

CodePudding user response:

You don't need a loop, just use Get-Content -Wait:

$null = Get-Content "\\$Server\$RootDir\folder\anotherfolder\A-Log-File.log" -Wait |Where-Object { $_ -match 'Switching to status: STARTED' } |Select -First 1

Get-Content -Wait will continue outputting new lines written to the file until it's interrupted - luckily we can use Select -First 1 to stop the pipeline once we observe the string

CodePudding user response:

You said "loop" so why aren't you using a loop?


while ($true) {
    # (re)try
    $SEL = Select-String -Path "\\$Server\$RootDir\folder\anotherfolder\A-Log-File.log" -Pattern "Switching to status: STARTED"
    if ($SEL -ne $null)
    {
        Write-Host "FOUND: Switching to status: STARTED" -ForegroundColor Yellow -BackgroundColor DarkGreen
        # exit the loop
        break;
    }
    # wait
    Write-Host "**** Waiting 60 seconds for cache to build ****"
    Start-Sleep 1
}
  • Related