if ($using:Selectbox.checked) in PS job not working as expected

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 5 years and 11 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

if ($using:Selectbox.checked) in PS job not working as expected

Post by stevens »

Hi,

I have a Powershell form with a button and a checkbox next to it. When I press the button, it should run a PS job.
That works fine.
However, when I select the checbkox, it should run a different command, that does not work because it does not recognize the checbox being selected.


So this is the code
if ($using:checkbox.Checked) { rest of the command ...}
else { }

Everything works fine BUT the if (checkbox.checked) ...
Tried also with giving it as $arg[0] but that doesn't work either.

Thanks for your input.
S.
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: if ($using:Selectbox.checked) in PS job not working as expected

Post by davidc »

You are not able to access the controls from the Job because it is in a different runspace / thread. I recommend passing the Checked boolean value to the job as a parameter.
David
SAPIEN Technologies, Inc.
User avatar
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Re: if ($using:Selectbox.checked) in PS job not working as expected

Post by stevens »

Thanks. You mean this then?

if ($Args[0]) { rest of the command ...}
else { }

-ArgumentList $checkbox.Checked

or ($args[0] -eq $true)

Couldn't make that one work.
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: if ($using:Selectbox.checked) in PS job not working as expected

Post by davidc »

Are you using the Job Tracker control set? Just update the parameters in the job scriptblock:
  1. -JobScript {
  2.         #--------------------------------------------------
  3.         #TODO: Set a script block
  4.         #Important: Do not access form controls from this script block.
  5.    
  6.         Param([bool]$Checked)#Pass any arguments using the ArgumentList parameter
  7.    
  8.         if($Checked -eq $true) { ... }
  9.        
  10.         #--------------------------------------------------
  11.     }
David
SAPIEN Technologies, Inc.
User avatar
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Re: if ($using:Selectbox.checked) in PS job not working as expected

Post by stevens »

Ok, thanks.
Couldn't make that one work either. Let me copy paste full code, just to be sure.

So I added
$Checked = $checkboxComputerNetWorkScan.Checked

...
-JobScript {
. $args[0]
Param ([bool]$checkboxComputerNetWorkScan.Checked)

...
-ArgumentList $ScriptPathNetworkScan, $Checked
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: if ($using:Selectbox.checked) in PS job not working as expected

Post by jvierra »

Note the note in the code:

Code: Select all

        #TODO: Set a script block
        #Important: Do not access form controls from this script block.
User avatar
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Re: if ($using:Selectbox.checked) in PS job not working as expected

Post by stevens »

Thanks, I noticed that, but I do not see how I'm accessing the form directly in the scriptblock.
Before the scriptblock I set
$Checked = $checkboxComputerScan.Checked

The scriptblock itself then checks this value
-JobScript {
. $args[0]
Param ([bool]$Checked)

-ArgumentList $ScriptPathNetworkScan,$Checked
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: if ($using:Selectbox.checked) in PS job not working as expected

Post by jvierra »

Two arguments? Then use two parameters

Code: Select all

-ArgumentList $ScriptPathNetworkScan,$Checked
-JobScript {
Param (
     $ScriptPathNetworkScan,
     $Checked
)
User avatar
stevens
Posts: 493
Last visit: Mon Sep 19, 2022 12:23 am
Has voted: 2 times

Re: if ($using:Selectbox.checked) in PS job not working as expected

Post by stevens »

Thanks for your input, but I'm not getting fully howto pass the $Checked for $checkbox1.checked, since I should not invoke form items (?)
This is what I got, how do I check the $checkbox.checked then IN the job?

Code: Select all

	Add-JobTracker -Name 'Get Service' `
				   -JobScript {
		
		Param ([bool]$Checked)
		
		if ($checked)
		{
			(Get-Service -Name AdobeARMservice).name
		}
		else
		{
			(Get-Service -Name Winmgmt).name
		}
			
	}`
				   -CompletedScript {
		Param ($Job)
		
	}`
				   -ArgumentList $Checked
	
}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: if ($using:Selectbox.checked) in PS job not working as expected

Post by jvierra »

-ArgumentList $checkbox1.Checked

Pass the control property as your argument.
This topic is 5 years and 11 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