Author Posts

May 24, 2014 at 10:57 pm

Hi

I want to execute a script on a remote machine using Invoke-command. My script file accepts name arguments. Can someone help me how to use the Invoke-command with -Argumentlist and named parameters.

Invoke-command -computername localhost -filepath .\sample.ps1 -argumentlist (Need this part)

My script file accepts two parameters: .\sample.ps1 -foo "abc" -bar "xyz"

Regards,
Tulasi Krishna.

May 24, 2014 at 11:11 pm

Please check out "Example 11" in the help of Invoke-Command.

Get-Help Invoke-Command -Examples

It explains how this scenario works and shows the ArgumentList parameter in action.

Regards,
Daniel

May 25, 2014 at 12:03 am

Hi Daniel,

The example shows only normal parameters. But my script takes named parameters. How to pass named parameters there?

If I try like the below, I get an error

Invoke-command -ComputerName localhost -FilePath C:\scripts\Simple1.ps1 -ArgumentList -foo "C:\Temp\a.txt" -bar "C:\Temp\b.txt"

Error:

Invoke-Command : A positional parameter cannot be found that accepts argument 'C:\Temp\a.txt'.
At line:1 char:15
+ Invoke-Command <<<< -ComputerName localhost -FilePath C:\scripts\Simple1.ps1 -ArgumentList -foo C:\Temp\a.txt -bar C :\Temp\b.txt + CategoryInfo : InvalidArgument: (:) [Invoke-Command], ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.InvokeCommandCommand Regards, Tulasi Krishna.

May 25, 2014 at 3:37 am

Sorry I didn't read that you want to pass named parameter earlier this morning. Unfortunately Invoke-Command does not support this out of the box. I found a thread on StackOverflow with possible workarounds.

http://stackoverflow.com/questions/4225748/how-do-i-pass-named-parameters-with-invoke-command

Regards,
Daniel