2

I'm totally new to PowerShell. All I'm trying to do is call an .exe on a remote computer using named parameters.

$arguments = "-clientId TX7283 -batch Batch82Y7"
invoke-command -computername FRB-TER1 { Start-Process -FilePath "C:\Program Files (x86)\Acorne\LoadDen.exe" -ArgumemtList $arguments}

I get this error.

A parameter cannot be found that matches parameter name 'ArgumemtList'.
+ CategoryInfo: InvalidArgument: (:) [Start-Process], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound, Microsoft.PowerShell.Commands.StartProcessCommand
+ PSComputerName : FRB-TER1

ArgumentList probably doesn't like parameter names. Not sure.

zorrinn
  • 45
  • 1
  • 6

3 Answers3

3

This should do your work:

$arguments = "-clientId TX7283 -batch Batch82Y7"
invoke-command -computername FRB-TER1 {param($arguments) Start-Process -FilePath "C:\Program Files (x86)\Acorne\LoadDen.exe" -ArgumentList $arguments} -ArgumentList $arguments
David Aleu
  • 3,922
  • 3
  • 27
  • 48
Ranadip Dutta
  • 8,857
  • 3
  • 29
  • 45
  • Do I need -ArgumentList $arguments twice? Nontheless, still gives me the same error. – zorrinn Jul 14 '17 at 15:58
  • I removed the additional ArgumentList and tried. This time it says, Cannot validate argument on parameter 'ArgumentList'. The argument is null or empty. – zorrinn Jul 14 '17 at 16:05
  • @zorrinn: Thats not additional. Last arg list is for passing the values inside the scriptblock of invoke-command. Param same used for accepting it inside the block. And finally it will come to start-process arg list where you actually want to pass – Ranadip Dutta Jul 14 '17 at 16:52
0

Try this one:

 # Lets store each cmd parameter in an array
 $arguments = @()
 $arguments += "-clientId TX7283"
 $arguments += "-batch Batch82Y7"
 invoke-command -computername FRB-TER1 { 
     param (
        [string[]]
        $receivedArguments
     ) 

     # Start-Process now receives an array with arguments
     Start-Process -FilePath "C:\Program Files (x86)\Acorne\LoadDen.exe" -ArgumemtList $receivedArguments
  } -ArgumentList @(,$arguments) # Ensure that PS passes $arguments as array
Moerwald
  • 10,448
  • 9
  • 43
  • 83
0

To pass a local variable to a scriptblock executed remotely you can also use $Using:Varname (from Posh Version 3.0 on). See the help of Invoke-Command:

> help Invoke-Command -Full |Select-String -Pattern '\$using' -Context 1,7

     PS C:\> Invoke-Command -ComputerName Server01 -ScriptBlock {Get-EventLog
>    -LogName $Using:MWFO_Log -Newest 10}

     This example shows how to include the values of local variables in a
     command run on a remote computer. The command uses the Using scope
     modifier to identify a local variable in a remote command. By default, all
     variables are assumed to be defined in the remote session. The Using scope
     modifier was introduced in Windows PowerShell 3.0. For more information
     about the Using scope modifier, see about_Remote_Variables
  • If scriptblocks are nested you may have to repeat the $using:varname See this reference

So this should work too (untested)

$arguments = "-clientId TX7283 -batch Batch82Y7"
Invoke-Command -computername FRB-TER1 {Start-Process -FilePath "C:\Program Files (x86)\Acorne\LoadDen.exe" $Using:arguments}