JobStatus Button - 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.
Locked
danilv77
Posts: 6
Last visit: Thu Oct 13, 2022 10:00 am
Has voted: 1 time

JobStatus Button - Form

Post by danilv77 »

Hello!

I apologize I post a lot but I am just learning how to use the tool and have searched the forums for this issue but did not quite see what I was looking for on them.

I am attempting to use the start job button for running a backup and restore process. I read that within the script code block you use to start the job, you cannot use form controls. The start job works flawless for the backup portion but the restore portion has user input (check boxes) and without being able to insert those into the code block (if checkbox is checked run x function), what is the best way to work around this, a child form? Or is there a way to use a checkbox portion of the form within a different part of the job script? Thanks as always for your time.
BRT.jpg
BRT.jpg (85.65 KiB) Viewed 519 times
jvierra
Posts: 15335
Last visit: Fri Nov 25, 2022 7:06 pm
Answers: 27
Has voted: 4 times
Been upvoted: 30 times

Re: JobStatus Button - Form

Post by jvierra »

You need two buttons and two independent scripts. The parameters collected become the "$Arguments" list.
danilv77
Posts: 6
Last visit: Thu Oct 13, 2022 10:00 am
Has voted: 1 time

Re: JobStatus Button - Form

Post by danilv77 »

Hello!

I do have two buttons that are start job designated, Backup and Restore. The problem I run into is with Restore it doesn't appear to be running the script when it is pressed (it doesn't perform any action and is just immediately done).
The backup code (snipped):

Code: Select all

$buttonStartJob_Click={
	
	$buttonStartJob.Enabled = $false
	
	#Create a New Job using the Job Tracker
	$paramAddJobTracker = @{
		Name	  = 'BackupJob'
		JobScript = {
			#--------------------------------------------------
			#TODO: Set a script block
			#Important: Do not access form controls from this script block.
			
			Param ($Argument1) #Pass any arguments using the ArgumentList parameter
			
			for ($i = 0; $i -lt 50; $i++) { Start-Sleep -Milliseconds 100 }
			##Creating Backup Folder
			$FolderName = "$env:HOMESHARE\TXBackup"
			Add-Type -AssemblyName PresentationFramework
			if ([System.IO.Directory]::Exists($FolderName))
			{
				$msgBoxInput = [System.Windows.MessageBox]::Show('A current backup exists, would you like to delete the current backup? (NOTE: This action cannot be undone)', 'Confirmation', 'YesNo')
				switch ($msgBoxInput)
The restore is (snipped):

Code: Select all

$buttonStartJob2_Click={
	
	$buttonStartJob2.Enabled = $false
	
	#Create a New Job using the Job Tracker
	$paramAddJobTracker = @{
		Name	  = 'RestoreJob'
		JobScript = {
			#--------------------------------------------------
			#TODO: Set a script block
			#Important: Do not access form controls from this script block.
			
			Param ($Argument1) #Pass any arguments using the ArgumentList parameter
			
			for ($i = 0; $i -lt 50; $i++) { Start-Sleep -Milliseconds 100 }
			
			if ($checkboxChrome.Checked -eq $true)
			{
				##Restoring Chrome Bookmarks
				$rbookmarks = "$env:HOMESHARE\TXBackup\Chrome\Bookmarks"
				#Copy-Item $rbookmarks "$env:userprofile\Appdata\Local\Google\Chrome\User Data\Default\Bookmarks" -Recurse -Force
				Copy-Item $rbookmarks "C:\Test" -Recurse -Force
			}
			if ($checkboxOtlksignatures.Checked -eq $true)
			{
				##Restoring Outlook Signatures
				$rosignature = "$env:HOMESHARE\TXBackup\OutlookSignatures\*"
				#Copy-Item $rosignature "$env:userprofile\Appdata\Roaming\Microsoft\Signatures\" -Recurse -Force
				Copy-Item $rosignature "C:\Test" -Recurse -Force
			}
			if ($checkboxOffnrmdotm.Checked -eq $true)
I am able to execute this code without the job parameters but when I insert it into the second job code it basically just says done right away and skips all the code and doesn't execute anything within the block.
jvierra
Posts: 15335
Last visit: Fri Nov 25, 2022 7:06 pm
Answers: 27
Has voted: 4 times
Been upvoted: 30 times

Re: JobStatus Button - Form

Post by jvierra »

You cannot use any forms controls in a JobScript. You must pass all arguments via the provided "$Argments" parameter.
danilv77
Posts: 6
Last visit: Thu Oct 13, 2022 10:00 am
Has voted: 1 time

Re: JobStatus Button - Form

Post by danilv77 »

Okay, that is what I was thinking. Is there some additional documentation/resources on how to manage using $Arguments to pass those? I am fairly new to Powershell and I am tasked to build a GUI at the same time so there has definitely been a learning curve :(
jvierra
Posts: 15335
Last visit: Fri Nov 25, 2022 7:06 pm
Answers: 27
Has voted: 4 times
Been upvoted: 30 times

Re: JobStatus Button - Form

Post by jvierra »

That constitutes multiple questions that can't be answered easily. Here is a link that will give the answers along with examples:

https://www.sapien.com/books_training/W ... werShell-4

The following may also be helpful: https://info.sapien.com/index.php/guis/ ... sive-forms

You can pass the properties of any control or group of controls in the $Arguments list.
Locked