Passing arguments to Job

Ask your PowerShell-related questions, including questions on cmdlet development!
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 4 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.
Locked
User avatar
Shelltastic
Posts: 65
Last visit: Mon Feb 19, 2024 11:31 am

Passing arguments to Job

Post by Shelltastic »

Using the "Button - Start-Job" control set in powershell studio, I am trying to pass multiple arguments to a job. I have a working script using one argument, but it seems to fail when I try to pass multiple arguments to it. Example given below using just one argument;

Working code...

Code: Select all

$Creds = Get-Credential

$Job1 = @{
Name	  = 'Job1'
JobScript = {
	Param ($Creds)
				
	$s = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://domain.com/powershell -Credential $Creds -Authentication Kerberos -ErrorAction SilentlyContinue
	Import-Module (Import-PSSession $s -AllowClobber -DisableNameChecking) -DisableNameChecking
				
	for ($i = 0; $i -lt 50; $i++) { Start-Sleep -Milliseconds 100 }
				
}
ArgumentList = $Creds
CompletedScript = {
	Param ($Job)
	$results = Receive-Job -Job $Job
}
UpdateScript = {
	Param ($Job)
				
}

}
User avatar
mxtrinidad
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

Re: Passing arguments to Job

Post by mxtrinidad »

Simply, the ArgumentList variable is an array type, so you can a list of arguments separated by a comma.

Example:

Code: Select all

ArgumentList = ($Path, $SearchFilter)
StartJob_Arglist_2019-05-22_17-24-56.png
StartJob_Arglist_2019-05-22_17-24-56.png (49.8 KiB) Viewed 3122 times
User avatar
Shelltastic
Posts: 65
Last visit: Mon Feb 19, 2024 11:31 am

Re: Passing arguments to Job

Post by Shelltastic »

In the jobscript, for the "Param", you have in your example "$Arg1" and "$Arg2", wouldn't that have to be the actual names of the arguments you listed in the "ArgumentList", in your example "$Path" and "$SearchFilter"? Or would literally "$Arg1" and "$Arg2" work?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Passing arguments to Job

Post by jvierra »

No. The list is applied in the order of the arguments in the "Param" statement. THe passed list should be the same types, order and number as the Param list.

The Param statement can use any names you like and should use names that are usefule and informative in the context of the scriptblock or function.

Note that a scriptblock is just a simple function without a name. A simple function is just a scriptblock with a name. THis does not completely apply to "Advanced functions".

Example:

Code: Select all

-ArgumentList = $Creds, $textboxPath.Text `
JobScript = {
    Param (
        $Creds,
        [string]$Path
    )
    
    $s = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://domain.com/powershell -Credential $Creds -Authentication Kerberos -ErrorAction SilentlyContinue
    Import-Module (Import-PSSession $s -AllowClobber -DisableNameChecking) -DisableNameChecking
    
    for ($i = 0; $i -lt 50; $i++) { Start-Sleep -Milliseconds 100 }
    
}
User avatar
mxtrinidad
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

Re: Passing arguments to Job

Post by mxtrinidad »

First, the sample code provided works.

You can download the sample code at: https://www.sapien.com/blog/2019/05/01/ ... plication/

Variables $Arg1 and $Arg2 will be substitute by the Argumentlist in the order you place your other variables:
Arg1 , Arg2 , ... (more Arguments).
ArgumentList = ($Path, $SearchFilter)

:)
This topic is 4 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.
Locked