PS form: test-connection -computer $computername freezes form

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 6 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
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

PS form: test-connection -computer $computername freezes form

Post by stevens »

To help you better we need some information from you.

*** Please fill in the fields below. If you leave fields empty or specify 'latest' rather than the actual version your answer will be delayed as we will be forced to ask you for this information. ***

Product, version and build: 2017, 5.4.136
32 or 64 bit version of product: 64
Operating system: W2K12R2
32 or 64 bit OS: 64

*** Please add details and screenshots as needed below. ***

Hi,

I have a form which manages computers: check processes, services, event viewer, get users logged on etc ....
Now when the computer is not online, it 'd like to have a textbox showing "$computername offline" or "$computernam online"
Made this and it works fine. However, since it is continuously running, it freezes the form. So I'd need to make a job for it.
There I'm stuck, because the job should just keep on running in the background each time ... and no output to be generated because there is no output.

Below what I have so far. Please advise.
J.

Code: Select all


		Add-JobTracker -Name 'CheckStatusComputer' `
					   -JobScript {
	
						$SleepTimeOut = 3
			Do
			{
				$available = $Null
				$notavailable = $Null
				
				if (Test-Connection -ComputerName $using:computername -Count 1 -ea silentlycontinue)
				{
					
					$computerlabelonlineoffline.Text = "$ComputerName online"
					$computerlabelonlineoffline.ForeColor = "green"
				}
				else
				{
					$computerlabelonlineoffline.Text = "$ComputerName OFFLINE"
					$computerlabelonlineoffline.ForeColor = "red"
					$computerlabelonlineoffline.Font = "bold"
				}
				
				sleep $SleepTimeOut
				
			}
			while ($Exit -ne $True)
			
		}`
					   -CompletedScript {
			Param ($Job)
			$Result = Receive-Job -Job $Job
			Invoke-Item $Result
		
					   -UpdateScript {
			Param ($Job)
		}`
					   
		
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: PS form: test-connection -computer $computername freezes form

Post by davidc »

[TOPIC MOVED TO THE POWERSHELL GUIS FORUM BY MODERATOR]
David
SAPIEN Technologies, Inc.
User avatar
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Re: PS form: test-connection -computer $computername freezes form

Post by stevens »

Can make it work outside Powershell Studio but not inside. This works outside PS Studio:

Code: Select all

$jobname = 'CheckStatusComputer'
if (get-job -Name $jobname) { get-job -Name $jobname | stop-job  ; get-job -Name $jobname | remove-job }
Start-Job -Name $jobname  -ScriptBlock {
				
        $SleepTimeOut = 3
        Do
        {
          $available = $Null
          $notavailable = $Null
					
          if (Test-Connection -ComputerName $using:computername -Count 1 -ea silentlycontinue)
          {
						
            Add-Content C:\result.txt -Value "$using:ComputerName online"
          }
          else
          {
            Add-Content C:\result.txt  -Value "$using:ComputerName offline"

          }
					
          sleep $SleepTimeOut
          					
        }
        while ($Exit -ne $True)
      } 
but when I try to write to label, it does not work:
$computerlabelonlineoffline.Text = "$using:ComputerName OFFLINE"
or
($using:computerlabelonlineoffline).Text = "$ComputerName OFFLINE"
User avatar
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Re: PS form: test-connection -computer $computername freezes form

Post by stevens »

So nobody any ideas at all?
User avatar
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Re: PS form: test-connection -computer $computername freezes form

Post by stevens »

So because of the misunderstanding in a previous post (to which I cannot reply because it is locked) which I had with jvierra, nobody answers this post anymore and nobody will in the future?
I explained jvierra what was happening in a private message but didn't hear back.

Found this in another post which makes clear that there IS an answer to my question:
viewtopic.php?t=4542

"The simple answer is NO. You cannot have a job update a form. All output from a job is captured and stored for later retrieval. You could have a timer on the form poll the jobe and display the rresults when it is completed."

So just want to know if need to stop posting questions from now on, let me know. Thanks.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: PS form: test-connection -computer $computername freezes form

Post by jvierra »

Sorry but I have received no private message form you. You last posted that you wanted me to not reply to your posts so I haven't replied.

This is a volunteer community forum. Most people who volunteer here have other jobs and may not always be available.

I am glad you found a solution to your issues.
User avatar
localpct
Posts: 397
Last visit: Thu Oct 27, 2022 5:57 am

Re: PS form: test-connection -computer $computername freezes form

Post by localpct »

I'm not sure if you've read this https://www.sapien.com/blog/2012/05/16/ ... ive-forms/

but it states you cannot access the form controls in the job function, you must do it using the update-jobtracker script

I personally feel the example given in the blog post is too plain jane and they should use a real world example ie

ping a computer in the form
update a textbox and step a progress bar while passing some arguments would totes be amazing
User avatar
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Re: PS form: test-connection -computer $computername freezes form

Post by stevens »

jvierra, strange. I did send you a private message but cannot find it in sent messages.
To make a long story short: I wrongly assumed you closed the post after posting an answer which was cryptic to me. Interpreted this as "your questions are foolish, I'll block them as from now.

Anyhow, jumped the conclusions. Water under the bridge as far as I'm concerned.
-
Haven't figured this out yet. I can make a job write to a file which mentions if a host is up or down (test-connection $computername). So writing to a from won't work.
How I can work with a timer job to query the output, f.e. every 5 seconds and write that to the form label ($computer is up/down) is really unclear to me though.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: PS form: test-connection -computer $computername freezes form

Post by jvierra »

The post by localpct should show you how to do this.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: PS form: test-connection -computer $computername freezes form

Post by jvierra »

Here is the easiest to check for a file repeatedly.
Attachments
Test-TimerTest.psf
(18.9 KiB) Downloaded 120 times
This topic is 6 years and 6 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