I am going to show you the way I do it, passing a file with -FilePath
and passing the parameters with -ArgumentList
I create a function that is going to contain the code I want to execute.
#remote_functions.ps1
param($command, $param1, $param2, $param3, $param4, $param5)
$ScriptVersion = "1.0"
function Write5Strings($string1, $string2, $string3, $string4, $string5)
{
Write-Host "String1: $string1"
Write-Host "String2: $string2"
Write-Host "String3: $string3"
Write-Host "String4: $string4"
Write-Host "String5: $string5"
throw "ERROR"
}
try
{
&$command $param1 $param2 $param3 $param4 $param5
}
catch [System.Exception]
{
Write-Error $_.Exception.ToString()
exit 1
}
And I invoke it this way:
$server="server"
$remotingPort=81
$job = Invoke-Command -AsJob -Port $remotingPort -ComputerName $server -FilePath ".\remote_functions.ps1" -ArgumentList @("Write5Strings", "apple", "banana", "orange", "pear", "watermelon")
Check also this question: How do I pass named parameters with Invoke-Command?