0

How to use -ArgumentList with Invoke-Command to pass flags to a script?

# File: ./setup.ps1

param(
    [Parameter(Mandatory=$false)]   
    [alias("force")]   
    [switch]$opt_force
)

if ($opt_force) {
    write-host "FORCING something!"
}
write-host "Done"

Powershell Command Line:

PS> Invoke-Command  -Computer pv3039  -FilePath ./setup.ps1 -ArgumentList "-force"

Error Message:

 positional parameter cannot be found that accepts argument '-force'.
    + CategoryInfo          : InvalidArgument: (:) [], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound
    + PSComputerName        : pv3039
pico
  • 1,660
  • 4
  • 22
  • 52
  • 2
    Just pass in $true. Since you only have the one parameter it will bind positionally. -Argumentlist $true – Doug Maurer Oct 18 '21 at 18:58
  • Unfortunately you can't pass in named parameters: https://stackoverflow.com/questions/4225748/how-do-i-pass-named-parameters-with-invoke-command – js2010 Oct 19 '21 at 01:50

1 Answers1

0

If you want to call a remote script, you can use Start-Process instead of Invoke-Command. Maybe you can try something like this.

Start-Process PowerShell -ArgumentList "YOUR_SCRIPT_PATH\setup.ps1 -Force" -NoNewWindow -Wait

This way it can accept parameters from the called script.

Nodah
  • 13
  • 3