You can pass arguments for the ScriptBlock not through the pipeline but as arguments in array as ArgumentList parameter to the Invoke-Command cmdlet. Then you will be able to access the arguments by $args variable inside your 'process' ScriptBlock.
function SomeFunc {
param ($x, $y, $sb)
Write-Host "Before";
Invoke-Command -ScriptBlock $sb -ArgumentList @("Some string", $x, ($x * $y))
Write-Host "After";
}
SomeFunc -x 4 -y 2 -sb { foreach ($a in $args) { Write-Host ("Parameter: $a") } }
The output would be
Before
Parameter: Some string
Parameter: 4
Parameter: 8
After
You can also include param() block inside your ScriptBlock. This way allows you to easily place additional restrictions on the arguments, such as strong typing
function SomeFunc {
param ($x, $y, $sb)
Write-Host "Before";
Invoke-Command -ScriptBlock $sb -ArgumentList @("Not x", $y, ($x * $y));
Write-Host "After";
}
SomeFunc -x 4 -y 2 -sb { param ([int]$a, $b, $c) Write-Host ("a is {0}, c is {1}, b is {2}" -f $a, $c, $b)}
The output shows the error
Before
Invoke-Command : Cannot convert value "Not x" to type "System.Int32". Error: "Input string was not in a correct format."
At line:4 char:5
+ Invoke-Command -ScriptBlock $sb -ArgumentList @("Not x", $y, ($x * $y));
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Invoke-Command], PSInvalidCastException
+ FullyQualifiedErrorId : InvalidCastFromStringToInteger,Microsoft.PowerShell.Commands.InvokeCommandCommand
After
-PipelineVariablethere? Doesn't it take the name of the variable to assign the pipeline variable to? Do you want to pass that result as an argument to the invoked block?