Passing named arguments to a script using -Argumentlist

This topic contains 3 replies, has 2 voices, and was last updated by Profile photo of Daniel Krebs Daniel Krebs 3 years, 1 month ago.

  • Author
    Posts
  • #15611
    Profile photo of Tulasi Moka
    Tulasi Moka
    Participant

    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.

  • #15612
    Profile photo of Daniel Krebs
    Daniel Krebs
    Moderator

    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

  • #15613
    Profile photo of Tulasi Moka
    Tulasi Moka
    Participant

    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.

  • #15614
    Profile photo of Daniel Krebs
    Daniel Krebs
    Moderator

    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

You must be logged in to reply to this topic.