Passing parameters while using invoke-command

I need to use invoke-command to execute a PowerShell script as a job and to pass a list of values.
I found several samples in forums and blogs but none works for me.
My code should look like:

$name = "Jojn"
$familyname = "Doe"
$age = 40
invoke-command -FilePath c:\myscripts\script1.ps1 -Argumentlist $name, $familyname, $age -Asjob

while the script to be invoked (c:\myscripts\script1.ps1) should look like

param($myname, $myfamilyname, $myage)
... 

The script should run with PowerShell 4 but working with PowerShell 2 whould be a plus.
Can anybody provide an hint?
Regards
marius

Hi Marius,
Use get-help to look at the usage for invoke-command. You will see there are 3 different parameter sets that use the parameters you have specified in your example

Invoke-Command [[-Session] ] [-FilePath]  [-ThrottleLimit ] [-AsJob] [-HideComputerName] [-JobName ] [-InputObject ] [-ArgumentList ]  []
Invoke-Command [[-ComputerName] ] [-FilePath]  [-Credential ] [-Port ] [-UseSSL] [-ConfigurationName ] [-ApplicationName ] [-ThrottleLimit ] [-AsJob] [-InDisconnectedSession] [-SessionName ] [-HideComputerName] [-JobName ] [-SessionOption ] [-Authentication  {Default | Basic | Negotiate | NegotiateWithImplicitCredential | Credssp | Digest | Kerberos}] [-EnableNetworkAccess] [-InputObject ] [-ArgumentList ]  []
Invoke-Command [-FilePath]  -Credential  -VMName  [-ThrottleLimit ] [-AsJob] [-HideComputerName] [-InputObject ] [-ArgumentList ]  []
Invoke-Command [-VMId]  [-FilePath]  -Credential  [-ThrottleLimit ] [-AsJob] [-HideComputerName] [-InputObject ] [-ArgumentList ]  []
Invoke-Command [[-ConnectionUri] ] [-FilePath]  [-Credential ] [-ConfigurationName ] [-ThrottleLimit ] [-AsJob] [-InDisconnectedSession] [-HideComputerName] [-JobName ] [-AllowRedirection] [-SessionOption ] [-Authentication  {Default | Basic | Negotiate | NegotiateWithImplicitCredential | Credssp | Digest | Kerberos}] [-EnableNetworkAccess] [-InputObject ] [-ArgumentList ] []
Invoke-Command [-FilePath]  -ContainerId  [-ThrottleLimit ] [-AsJob] [-HideComputerName] [-JobName ] [-RunAsAdministrator] [-InputObject ] [-ArgumentList ]  []
Invoke-Command [-FilePath]  -ContainerName  [-ThrottleLimit ] [-AsJob] [-HideComputerName] [-JobName ] [-RunAsAdministrator] [-InputObject ] [-ArgumentList ]  []

As a result Powershell cannot determine which parameter set to use. You should be getting an error indicating this.

Invoke-Command : Parameter set cannot be resolved using the specified named parameters.
At line:4 char:1
+ invoke-command -FilePath c:\temp\script1.ps1 -Argumentlist @($name, $ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Invoke-Command], ParameterBindingException
    + FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.PowerShell.Commands.InvokeCommandCommand

You need to define additional parameters for the cmdlet such as -ComputerName or -Session

Many thanks.
Let me ask for some further details:

  • I see the use of [-ArgumentList ]: given I need to pass 3 parameters like in my sample cose, what is the right way to specify them?
  • Is -FilePath the right way to use a file instead of a scriptblock?
  • Is invoke-command the right way to run a scriptblock or a script to be executed in background?
    Regards
    marius

Hi Marius,
You can pass arguments like this

Invoke-Command -ComputerName rwlvri001sv0022 -ScriptBlock {Get-Process -Name $args[0],$args[1]} -ArgumentList "lsass","svchost"

Invoke-Command is usually used if you want to run something on a remote machine. If you just want to run something in the background on the local machine, you might want to look into Start-Job or using RunSpaces.