Passing arguments to Job

Ask your Windows 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.
Locked
User avatar
PS_Ross
Posts: 58
Joined: Thu Mar 01, 2018 7:46 am

Passing arguments to Job

Post by PS_Ross » Wed May 22, 2019 12:29 pm

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
Site Admin
Posts: 368
Joined: Sun Mar 03, 2013 12:42 pm

Re: Passing arguments to Job

Post by mxtrinidad » Wed May 22, 2019 2:28 pm

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 1565 times

User avatar
PS_Ross
Posts: 58
Joined: Thu Mar 01, 2018 7:46 am

Re: Passing arguments to Job

Post by PS_Ross » Thu May 23, 2019 6:03 am

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: 13992
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Passing arguments to Job

Post by jvierra » Thu May 23, 2019 6:12 am

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
Site Admin
Posts: 368
Joined: Sun Mar 03, 2013 12:42 pm

Re: Passing arguments to Job

Post by mxtrinidad » Thu May 23, 2019 7:34 am

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)

:)

Locked