passing function or variables to Start-Job scriptblock

This forum can be browsed by the general public. Posting is limited to current SAPIEN license holders with active maintenance and does not offer a response time guarantee.
Forum rules
DO NOT POST LICENSE NUMBERS, ACTIVATION KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM.
Only the original author and our tech personnel can reply to a topic that is created in this forum. If you find a topic that relates to an issue you are having, please create a new topic and reference the other in your post.

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 9 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.
User avatar
laurensdekock
Posts: 11
Last visit: Fri Aug 08, 2014 8:18 am

passing function or variables to Start-Job scriptblock

Post by laurensdekock »

I am kind of new to programming and powershell so please bear with me. I've solved every problem so far and read so many examples but I can't figure this one out:

I can't seem to use a function, or variable in the scriptblock of a Job. I understand the scope issue and that it's another process, and I think I need to pass the variables with the -argumentlist? But I can't seem to get this working. Also not when using $script:Uefi

So I actually have the following issues:

- Calling the function in a Job
- Passing variables set by e.g. a combobox to a Job to do further If-Then code

My example function:
PowerShell Code
Double-click the code block to select all.
function check-uefi
{
	if ($comboboxMediaplayerkeuze.Text -eq "MSI Windbox DC100")
	{
		$UEFI = "false"
	}
	else
	{
		$UEFI = "true"
	}
The Start-Job using -argumentlist to pass variables
PowerShell Code
Double-click the code block to select all.
#Start the Job
	$job = Start-Job -Name $Name -ScriptBlock $JobScript -ArgumentList $UEFI

The -scriptblock code: I was hoping $p1 would have the value of $UEFI
PowerShell Code
Double-click the code block to select all.
#Create a New Job using the Job Tracker
		Add-JobTracker -Name "JobName" `
					   -JobScript {
			#--------------------------------------------------
			Param ($p1)#Pass any arguments using the ArgumentList parameter
			
			# trying to call the check-uefi function
			check-uefi
			
			if ($p1 -eq "true")
			{
				$testvar = "check to see is value can be read: True"
				diskpart /s m:\scripts\createpartitions-uefi.txt | Out-String | Out-File x:\log.txt -Append
				manage-bde -on w: | Out-String | Out-File x:\log.txt -Append
			 	Expand-WindowsImage -imagePath m:\images\windows-nuc-i3.wim -ApplyPath w:\ -Index:1 | Out-String | Out-File x:\log.txt -Append
			}
			else
			{
				diskpart /s m:\scripts\createpartitions-non-uefi.txt
				$testvar = "check to see is value can be read: False"
			}
User avatar
Ferdinand Rios
Posts: 373
Last visit: Fri Sep 16, 2022 1:24 pm

Re: passing function or variables to Start-Job scriptblock

Post by Ferdinand Rios »

Unfortunately, this is not the proper forum for answers regarding actual scripting questions; this forum is reserved for product technical questions. I would suggest posting your question on the ScriptingAnswers forum where you have a better chance of getting a helpful answer.

Thank you.
F.G. Rios
User avatar
laurensdekock
Posts: 11
Last visit: Fri Aug 08, 2014 8:18 am

Re: passing function or variables to Start-Job scriptblock

Post by laurensdekock »

Sorry, wasn't aware of this. I'll post it there. you can delete this thread.
This topic is 9 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.