Home > Blockchain >  Foreach parallel Missing an argument for parameter 'xxx'. Specify a parameter of type 
Foreach parallel Missing an argument for parameter 'xxx'. Specify a parameter of type 

Time:10-03

i'm using a parallel foreach in a powershell script. I'm getting a problem to pass external variable inside the loop. The code is following

[CmdletBinding()]
param (
    $var1,
    $var2,
    $var3,
    $var4
)


$MyArr | ForEach-Object -Parallel {
 
   
      Invoke-Expression ".\myscript.ps1 -var1 $var1 -var2 $var2 -var3 $var3 -var4 $var4"


}

when execute it i'm getting

myscript.ps1: Missing an argument for parameter 'var1'. Specify a parameter of type 'System.Object' and try again.

There is a way to fix it?

REgards Thanks

CodePudding user response:

Use the using: special scope modifier to make PowerShell copy the values to the underlying runspace:

$MyArr | ForEach-Object -Parallel {
    .\myscript.ps1 -var1 $using:var1 -var2 $using:var2 -var3 $using:var3 -var4 $using:var4
}
  • Related