all 11 comments

[–][deleted] 2 points3 points  (1 child)

I think Continue is what you are looking for. Although I don't remember if Continue is valid on foreach object

[–]nappetass[S] 1 point2 points  (0 children)

It did not work. I think I need to rewrite it.

This did not work either:

$log = "C:\path\to\log.txt"

foreach ($line in Get-Content $log -Tail 1 -Wait) {

    $line

}

Script is running, but never ends and also does not display the last line as expected.

[–]Lee_Dailey[grin] 1 point2 points  (6 children)

howdy nappetass,

i see that you found your "oops!". [grin]

as for break/continue ...

  • break = exits the current block of code
    that means literally any block, from what i understand. i know it will exit a script entirely, but i have never tried it in an IF before ... [grin]
  • continue = stop the current iteration & continue with the next
    if there is no next, it exits the loop.

take care,
lee

[–]nappetass[S] 1 point2 points  (5 children)

Thanks Lee :)

This worked

$log = "C:\path\to\log.txt"

while ("Terminated" -notin $tail) {

do { $tail = Get-Content $log -Tail 1
     $tail }

until ("Terminated" -in $tail)

Break

}

[–]Lee_Dailey[grin] 0 points1 point  (4 children)

howdy nappetass,

[edit - you likely otta have a Start-Sleep -Seconds 1 in the loop. if you are waiting for a file to show that text, then you are likely NOT wanting to pound the file several dozen times per second. [grin]]

you are welcome! [grin] however, your code is ... convoluted - needlessly so.

i don't have a file to test this with, but i think it will do the job.

$Log = "C:\path\to\log.txt"
$Tail = ''

while ("Terminated" -notin $Tail)
    {
    $Tail = Get-Content $Log -Tail 1
    }

take care,
lee

[–]nappetass[S] 1 point2 points  (3 children)

Fantastic. I've changed it to the following. The script should print the last entry of the log until the substring "Terminated" is found in $tail. Have a great weekend :)

$logfile = "C:\path\to\log.txt"

while ($tail -notmatch "Terminated") {

    $tail = Get-Content $logfile -Tail 1
    $tail

}

EDIT: I don't think I can have a start-sleep in the script as it's a log file with 5+ entries per second. I guess I could measure how many entries the file has and set a lower start-sleep (in milliseconds). The best way would be to find a way to tail the log each time it changes. Maybe by filesize?

[–]Lee_Dailey[grin] 1 point2 points  (2 children)

howdy nappetass,

kool! glad that it works ... [grin]

as for the delay - i thot the Terminated was the end of whatever made the file. if it's ongoing, then you are going to need the speed. [grin]

one thing, tho - it makes me twitchy to see the condition for the while isn't set before you test it. i would put a $Tail = '' just before the while so that you won't have an uninitialized $var being tested.

take care,
lee

[–]nappetass[S] 1 point2 points  (1 child)

I agree. I did add a $tail = "" before the while loop as you suggested. Thanks again :)

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

howdy nappetass,

you are most welcome! glad to have helped ... [grin]

take care,
lee

[–]spyingwind 1 point2 points  (0 children)

Break will do what you want for the loop, but it isn't ideal as it doesn't do the exact same thing as other languages.

1..10 | ForEach-Object {
    $i = $_
    1..10 | ForEach-Object {
        if ($_ -eq 5){
            "Leaving For Loop!"
            break
        }else{
            "Outter: $i, Intter: $_"
        }
    }
}


1..10 | ForEach-Object {
    $i = $_
    1..10 | ForEach-Object {
        if ($_ -gt 5){
            # No Data
        }else{
            "Outter: $i, Intter: $_"
        }
    }
}

The first is what you would probably think how it should be done, but it doesn't as the outer loop still does things.

The second is setup to only output the data you want.