Monitor A Text File From Windows PowerShell In Real Time with OutPut Text Box

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 6 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Monitor A Text File From Windows PowerShell In Real Time with OutPut Text Box

Post by jvierra »

All code in a form must be run in an event. With a job you will need to use a timer to poll the job. The output of the job will need to be parsed to eliminate the elements of the received object to get only what you need.

See the following for an explanation of how jobs can be used in a form.

http://info.sapien.com/index.php/guis/g ... sive-forms
User avatar
ramses147
Posts: 110
Last visit: Tue Dec 05, 2023 7:11 am
Been upvoted: 1 time

Re: Monitor A Text File From Windows PowerShell In Real Time with OutPut Text Box

Post by ramses147 »

I followed and studied the link you sent me.
But I can not figure out how to adapt it to my need :-( could you write a short example for the textbox?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Monitor A Text File From Windows PowerShell In Real Time with OutPut Text Box

Post by jvierra »

There is no such thing as a short example of that code.

Add a job and a timer then set the timer tick to poll the job and add it to the textbox.

Don't try to build job tracker just use the post to learn how the JobTracker is designed and then use the components you need.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Monitor A Text File From Windows PowerShell In Real Time with OutPut Text Box

Post by jvierra »

Here is the simplest example I can think of:
  1. $form1_Load={
  2.     $timer1.Interval = 500
  3.     $timer1.Start()
  4. }
  5.  
  6. $buttonPingTracker_Click={
  7.     $sb = { Get-Content d:\scripts\pinger.txt -Wait -Tail 1 }
  8.     Start-Job -Name pinger -ScriptBlock $sb
  9. }
  10.  
  11. $timer1_Tick={
  12.     if($results = Receive-Job pinger -ErrorAction 0){
  13.         $textbox1.Lines += $results
  14.         $textbox1.Select($textbox1.Text.Length,1)
  15.         $textbox1.ScrollToCaret()
  16.     }
  17. }
  18.  
  19. $form1_FormClosing=[System.Windows.Forms.FormClosingEventHandler]{
  20.     Stop-Job pinger -ErrorAction 0
  21.     Remove-Job pinger -ErrorAction 0
  22. }
User avatar
ramses147
Posts: 110
Last visit: Tue Dec 05, 2023 7:11 am
Been upvoted: 1 time

Re: Monitor A Text File From Windows PowerShell In Real Time with OutPut Text Box

Post by ramses147 »

I tryed with your code but i unable to show textbox content :cry:
  1. $Form1 = New-Object System.Windows.Forms.Form    
  2. $Form1.Size = New-Object System.Drawing.Size(790,980)
  3.  
  4. $Form1.FormBorderStyle = 'FixedDialog'
  5. $Form1.MaximizeBox = $false
  6.  
  7. $Icon = [system.drawing.icon]::ExtractAssociatedIcon($PSHOME + "\powershell.exe")
  8. $Form1.Icon = $Icon
  9.  
  10. $Form1.Text = "Log View"
  11.  
  12. ############################################## OutPutBox
  13.  
  14. $textbox1 = New-Object System.Windows.Forms.TextBox
  15. $textbox1.Location = New-Object System.Drawing.Size(20,20)
  16. $textbox1.Size = New-Object System.Drawing.Size(680,900)
  17. $textbox1.MultiLine = $True
  18. $textbox1.ReadOnly = $True
  19. $textbox1.Font = New-Object System.Drawing.Font("Calibri",11,[System.drawing.FontStyle]::Bold)
  20. $textbox1.ForeColor = [Drawing.Color]::Green
  21. $textbox1.ScrollBars = "Vertical"
  22. $Form1.Controls.Add($textbox1)
  23.  
  24. $form1_Load={
  25.     $timer1.Interval = 500
  26.     $timer1.Start()
  27. }
  28.  
  29. $buttonPingTracker_Click={
  30.     $sb = { Get-Content C:\script\PingLog.csv -Delimiter -Wait -Tail 1 }
  31.     Start-Job -Name pinger -ScriptBlock $sb
  32. }
  33.  
  34. $timer1_Tick={
  35.     if($results = Receive-Job pinger -ErrorAction 0){
  36.         $textbox1.Lines += $results
  37.         $textbox1.Select($textbox1.Text.Length,1)
  38.         $textbox1.ScrollToCaret()
  39.     }
  40. }
  41.  
  42. $form1_FormClosing=[System.Windows.Forms.FormClosingEventHandler]{
  43.     Stop-Job pinger -ErrorAction 0
  44.     Remove-Job pinger -ErrorAction 0
  45. }
  46.  
  47. $Form1.Add_Shown({$Form1.Activate()})
  48. [void] $Form1.ShowDialog()
User avatar
ramses147
Posts: 110
Last visit: Tue Dec 05, 2023 7:11 am
Been upvoted: 1 time

Re: Monitor A Text File From Windows PowerShell In Real Time with OutPut Text Box

Post by ramses147 »

With this code i can able to read the content of file, but the content doesn't update :x
  1. $timer1 = new-object timers.timer
  2. #$form_Load={
  3.     $timer1.Interval = 500
  4.     $timer1.Start()
  5. #}
  6.  
  7.  
  8. $sb = { Get-Content c:\script\PingLog.csv -Wait -Tail 1 }
  9. Start-Job -Name pinger -ScriptBlock $sb
  10.    
  11.  
  12. #$timer1_Tick={
  13.     if($results = Receive-Job pinger -ErrorAction 0){
  14.         $outputBox.Lines += $results
  15.         $outputBox.Select($outputBox.Text.Length,1)
  16.         $outputBox.ScrollToCaret()
  17.     }
  18. #}
  19.  
  20.  
  21. $form_FormClosing=[System.Windows.Forms.FormClosingEventHandler]{
  22.     Stop-Job pinger -ErrorAction 0
  23.     Remove-Job pinger -ErrorAction 0
  24. }
User avatar
ramses147
Posts: 110
Last visit: Tue Dec 05, 2023 7:11 am
Been upvoted: 1 time

Re: Monitor A Text File From Windows PowerShell In Real Time with OutPut Text Box

Post by ramses147 »

Find the problem !
With the help of Powershell Studio ! :D
Many thanks !
This topic is 6 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked