Pass arguments to a scriptblock in powershell

PowershellScriptblock

Powershell Problem Overview


I guess you can't just do this:

  $servicePath = $args[0]
  
  if(Test-Path -path $servicePath) <-- does not throw in here

  $block = {

        write-host $servicePath -foreground "magenta"

        if((Test-Path -path $servicePath)) { <-- throws here.

              dowork 
        }
  }

So how can I pass my variables to the scriptblock $block?

Powershell Solutions


Solution 1 - Powershell

Keith's answer also works for Invoke-Command, with the limit that you can't use named parameters. The arguments should be set using the -ArgumentList parameter and should be comma separated.

$sb = {
    param($p1,$p2)
    $OFS=','
    "p1 is $p1, p2 is $p2, rest of args: $args"
}
Invoke-Command $sb -ArgumentList 1,2,3,4

Also see here and here.

Solution 2 - Powershell

A scriptblock is just an anonymous function. You can use $args inside the scriptblock as well as declare a param block, for example

$sb = {
  param($p1, $p2)
  $OFS = ','
  "p1 is $p1, p2 is $p2, rest of args: $args"
}
& $sb 1 2 3 4
& $sb -p2 2 -p1 1 3 4

Solution 3 - Powershell

BTW, if using the script block to run in a separate thread (multi threaded):

$ScriptBlock = {
    param($AAA,$BBB) 
    return "AAA is $($AAA) and BBB is $($BBB)"
}

$AAA = "AAA"
$BBB = "BBB1234"    
$null = Start-Job $ScriptBlock -ArgumentList $AAA,$BBB

then yields:

$null = Start-Job $ScriptBlock -ArgumentList $AAA,$BBB    
Get-Job | Receive-Job
AAA is AAA and BBB is BBB1234

Solution 4 - Powershell

For anyone reading in 2020 who wants to use local variables in a remote session script block, starting in Powershell 3.0 you can use local variables directly in the scriptblock with the "$Using" scope modifier. Example:

$MyLocalVariable = "C:\some_random_path\"
acl = Invoke-Command -ComputerName REMOTEPC -ScriptBlock {Get-Acl $Using:MyLocalVariable}

Found in example 9 of https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/invoke-command?view=powershell-7

Solution 5 - Powershell

By default PowerShell won't capture variables for a ScriptBlock. You can explicitly capture by calling GetNewClosure() on it, however:

$servicePath = $args[0]

if(Test-Path -path $servicePath) <-- does not throw in here

$block = {

    write-host $servicePath -foreground "magenta"

    if((Test-Path -path $servicePath)) { <-- no longer throws here.

          dowork 
    }
}.GetNewClosure() <-- this makes it work

Solution 6 - Powershell

Three example syntax:

$a ={ 
  param($p1, $p2)
  "p1 is $p1"
  "p2 is $p2"
  "rest of args: $args"
}
//Syntax 1:
Invoke-Command $a -ArgumentList 1,2,3,4 //PS> "p1 is 1, p2 is 2, rest of args: 3 4"
//Syntax 2:
&$a -p2 2 -p1 1 3      //PS> "p1 is 1, p2 is 2, rest of args: 3"
//Syntax 3:
&$a 2 1 3              //PS> "p1 is 2, p2 is 1, rest of args: 3"

Solution 7 - Powershell

I know this article is a bit dated, but I wanted to throw this out as a possible alternative. Just a slight variation of the previous answers.

$foo = {
    param($arg)

    Write-Host "Hello $arg from Foo ScriptBlock" -ForegroundColor Yellow
}

$foo2 = {
    param($arg)

    Write-Host "Hello $arg from Foo2 ScriptBlock" -ForegroundColor Red
}


function Run-Foo([ScriptBlock] $cb, $fooArg){
    
    #fake getting the args to pass into callback... or it could be passed in...
    if(-not $fooArg) {
        $fooArg = "World" 
    }
    #invoke the callback function
    $cb.Invoke($fooArg);

    #rest of function code....
}

Clear-Host

Run-Foo -cb $foo 
Run-Foo -cb $foo 

Run-Foo -cb $foo2
Run-Foo -cb $foo2 -fooArg "Tim"

Solution 8 - Powershell

Other posibility: $a ={ param($p1, $p2) "p1 is $p1" "p2 is $p2" "rest of args: $args" } $a.invoke(1,2,3,4,5)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondexterView Question on Stackoverflow
Solution 1 - PowershellLars TruijensView Answer on Stackoverflow
Solution 2 - PowershellKeith HillView Answer on Stackoverflow
Solution 3 - PowershellJean-Claude DuBoisView Answer on Stackoverflow
Solution 4 - Powershelluser724067View Answer on Stackoverflow
Solution 5 - PowershellChris R. DonnellyView Answer on Stackoverflow
Solution 6 - PowershellchnnnView Answer on Stackoverflow
Solution 7 - PowershellSpaceGhost440View Answer on Stackoverflow
Solution 8 - PowershellkoekjesdoosView Answer on Stackoverflow