1

I'm quite new to PowerShell and I'm hoping to ask a question about a command I'm attempting to run.

I've read and read everything I can find on this so apologies in advance if I'm asking the impossible or something dumb.

From the Windows CLI on the remote computer I can run the following command; 'c:\config-files\app.exe foo /o /last' the exe generates an output file by reading the foo files and saves it as foo.txt.

app.exe doesn't exist in within the c:\config-files, when running it on the computer the app.exe is in the local env path within c:\main-app.

- The above is one of the key points here which has been addressed in replies below.

I've tried adding a path to the exe but that seems to be ignored when performing the following;

path='c:\main-app\'
& Invoke-Command -ComputerName foo -ScriptBlock { & cmd.exe /c "c:\config-files\app" } -ArgumentList 'foo', '/last', '/o'

The above fails (probably obvious to some!)

If I run:

Invoke-Command foo  -ScriptBlock {& cmd.exe /c "c:\main-app\app" }

The application runs in the PowerShell window, I just then seem to be unable to send Arguments to the application.

Invoke-Command -Computername foo -ScriptBlock {param ($myarg) "cmd.exe /c c:\main-app\app" $myarg } -ArgumentList 'foo', '/last', '/o'

This is the closest I think I've got, but it only reads one argument and that's being invoked from the documents and setting folder of the user attempting to execute the command and not that path of the binary.

I've tried many, many things to make this work but still don't seem to be able to get past this point, any help you can provide on this would be hugely appreciated.

Thanks in advance for your time.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Possible duplicate of [Powershell Execute remote exe with command line arguments on remote computer](https://stackoverflow.com/questions/9535515/powershell-execute-remote-exe-with-command-line-arguments-on-remote-computer) – henrycarteruk Feb 11 '19 at 11:20
  • Hey @JamesC. Thank you for the suggestion, I've followed a lot of that article; but the points about the path that I make above aren't answered in there. I guess I have a lot to learn about PowerShell! – Foolishstar Feb 11 '19 at 15:58

1 Answers1

2
  • You don't need cmd /c to invoke a console application (or any external program).

  • To access arguments passed to a script block from within the script, either use the automatic $Args array or declare parameters explicitly (as you've attempted with a single parameter).

  • You can use an array directly for passing its elements as individual arguments to an external program.

Invoke-Command -Computername foo -ScriptBlock { 
  c:\main-app\app $Args  # invoke app.exe, passing arguments through
} -ArgumentList 'foo', '/last', '/o'

Additionally, you mention wanting to interpret arguments that are file paths as relative to the directory in which the application is located; the simplest solution is to use a Set-Location command first:

Invoke-Command -Computername foo -ScriptBlock { 
  Set-Location c:\main-app
  .\app $Args 
} -ArgumentList 'foo', '/last', '/o'
mklement0
  • 382,024
  • 64
  • 607
  • 775