-1

I have a PowerShell script running on a remote machine. I have it writing data to a text file as it completes its work. Once it's done it writes a specific line.

I have this in the local PowerShell script to monitor that file on the remote machine:

Get-Content -Path $Path -Tail 0 -Wait

It is working great, but how do I tell it to stop monitoring once that specific line is reached?

I tired putting it into a do while loop, but it never releases to complete the do while.

Here is a link to a simpler version of what I am asking:

How to monitor a text file in realtime

The first answer is good, but I don't want to just look for a certain line. I want to write them all VIA Write-Host till that phrase then break from Get-Content and continue with the remaining parts of the script.

Here is what I finally ended up with. It is not pretty due to the way I exited the ForEach-Object.

Get-Content -Path $path -Tail 0 -wait | ForEach-Object{if($_ -match $word){write-host "- $_" ;cjklnsrvf } else {write-host "- $_"} }

I used a Try and Catch for the cjklnsrvf in the if statement above. This is done because ForEach-Object cannot use the break or continue statements. It seems that when piping a ForEach loop it is turned into (alias) the ForEach-Object cmdlet. The ForEach-Object cmdlet doesn't use the break and continue commands like a foreach loop.

If you use a break in a ForEach-Object it will immediately exit the whole script. There was one guy on one site that brought up loop death by garbage, and it indeed does work here as well.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
reddragon72
  • 191
  • 1
  • 3
  • 16
  • Make it 2 down votes. Stack Overflow is not a coding service. Dazzle the community with what you have already tried and what you have researched. Why should someone else write your code for you when you did not show any effort on your part to solve your own issue? – dfundako Dec 29 '15 at 18:58
  • because I cannot share the code! I would have to spend three hours to rewrite it just for you to see it and then it would not be doing what I am doing. It's simple I have a script writing data to a file, I need to read it as it comes in till a certain line.... There is no documentation on -tail or -wait and piping if statements are not working... – reddragon72 Dec 29 '15 at 19:07

4 Answers4

0
Get-Content $path -Tail 0 -Wait | foreach { if ($_ -eq "Specific Line") { Write-Output $_ ; break } }

Or just the break, obviously, if you have no use of the output.

zumalifeguard
  • 8,648
  • 5
  • 43
  • 56
notjustme
  • 2,376
  • 2
  • 20
  • 27
  • This is what I have..... Get-Content -Path $path -Tail 0 -wait | ForEach-Object{if($_ -match $word){write-host "- $_" ;continue }else{write-host "- $_"} } ....it works great but both break, quit, and continue exit the entire script. – reddragon72 Dec 29 '15 at 20:41
  • Righty! I don't think you mentioned anything about continued running of the script initially.. but then again, it does makes sense for something to actually to happen once that specific line is found. I need my sleep. :P – notjustme Dec 29 '15 at 21:55
  • Yea there is more to the script. Also just found another guy who ran the plain line without piping in a thread but that is a bit over done just for this. Also note how I exited the pipe, very odd that MS left that "bug" in there. Thanks for you input though as it helped reinforce where I was heading. – reddragon72 Dec 30 '15 at 02:52
0
Do {
    $content = get-content $path -tail 0 -ea 00 | where {$_ -like "*string found*"}
    Sleep -milliseconds 1000
} until($content)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Chand
  • 300
  • 3
  • 13
  • I had something similar but couldn't use it it because the file being written to can have 10 lines added in half a second and then no lines for over a min. It is running checks on a system and each check varies in length and why I went for the -wait statement – reddragon72 Dec 29 '15 at 20:43
0

UPDATE: Here is what I finally ended up with. It is not pretty due to the way I exited the foreach-object.

Get-Content -Path $path -Tail 0 -wait | ForEach-Object{if($_ -match $word){write-host "- $_" ;cjklnsrvf } else {write-host "- $_"} } 

I used a Try and Catch for the cjklnsrvf in the if statement above. This is done because ForEach-Object cannot use the break or continue statements. It seems that when piping a ForEach loop it is turned into(alias) the ForEach-Object cmdlet. The ForEach-Object cmdlet doesn't use the break and continue commands like a foreach loop. If you use a break in a ForEach-Object it will immediately exit the whole script. There was one guy on one site that brought up loop death by garbage and it indeed does work here as well.

reddragon72
  • 191
  • 1
  • 3
  • 16
-1
Invoke-Command -ComputerName xxx -Credential $cred -ScriptBlock {if(test-path 'c:\1.txt'){cat 'c:\1.txt'}else{'file not exist'}}
  • http://stackoverflow.com/questions/5203730/cut-off-text-in-string-after-before-seperator-in-powershell – a guest Dec 29 '15 at 19:44