Timer Counter

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 7 years and 2 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
User avatar
localpct
Posts: 397
Last visit: Thu Oct 27, 2022 5:57 am

Timer Counter

Post by localpct »

What I'm looking for is a way to display how many times a $timer1_tick has occurred. I don't care about the time. The $DisplayInfo is my text box and here is my script
  1. [TimeSpan]$span = $script:StartTime - (Get-Date)
  2.     if ($span.TotalSeconds -le 0)
  3.     {
  4.         If (test-Connection $computer -Quiet -Count 1)
  5.         {
  6.             $ipv4 = Test-Connection $computer -Count 1 | select IPV4Address | ft -HideTableHeaders | Out-String
  7.             $DisplayInfo.Lines += "$computer is online | $($ipv4.trim())" |  %display how many times the tick even has occurred here
  8.             $script:StartTime = (Get-Date).AddSeconds($TotalTime)
  9.             $DisplayInfo.SelectionStart = $DisplayInfo.Text.Length
  10.             $DisplayInfo.ScrollToCaret()
  11.             $DisplayInfo.ScrollBars = 'Vertical'
  12.         }
  13.        
  14.         else
  15.         {
  16.             $DisplayInfo.Lines += "$computer is not online"
  17.             $body = @"
  18.                 <html>
  19.                     <body>
  20.                         Firewall Server is down!!!!!
  21.                     </body>
  22.                 </html>
  23. "@
  24.            
  25.             $params = @{
  26.                 Body = $body
  27.                 BodyAsHtml = $true
  28.                 Subject = "Help me Chris you're my only hope!"
  29.                 From = "$env:USERNAME@domain.com"
  30.                 To = "networkguy@domain.com"
  31.                 Cc = "$env:USERNAME@domain.com"
  32.                 SmtpServer = 'firewall-domain.com'
  33.                
  34.             }
  35.             Send-MailMessage2 @params
  36.             $timer1.Stop()
  37.         }
  38.        
  39.     }
So for example, I want my textbox to look like this. Everything works besides the counter

firewall-domain.com is online | 192.168.0.6 | 1
firewall-domain.com is online | 192.168.0.6 | 2
firewall-domain.com is online | 192.168.0.6 | 3
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Timer Counter

Post by jvierra »

In the timer tick event just add the ticks to a variable.

$script:ticks+=1

Now you can just display the ticks any time you need to.
User avatar
localpct
Posts: 397
Last visit: Thu Oct 27, 2022 5:57 am

Re: Timer Counter

Post by localpct »

Seems too simple :)

Will update when I can
User avatar
localpct
Posts: 397
Last visit: Thu Oct 27, 2022 5:57 am

Re: Timer Counter

Post by localpct »

jvierra wrote:In the timer tick event just add the ticks to a variable.

$script:ticks+=1

Now you can just display the ticks any time you need to.
Nice work! Thanks!
This topic is 7 years and 2 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