4

I have a folder at C:\Folder that has files input.xml, output.xml and licensegenerator.exe. Licensegenerator.exe takes variables that we put into input.xml and creates a temporary license for one of our programs using the output.xml file. We typically do this via command line by navigating to the C:\Folder directory, then running the command:

LicenseGenerator.exe "C:\Folder\input.xml" "C:\Folder\output.xml"

I'm attempting to write a script to do the exact same thing in PowerShell, but I'm struggling... Here's what I have:

$inputtest = "C:\Folder\Input.xml"
$outputtest = "C:\Folder\Output.xml"
$licensegen = "C:\Folder\LicenseGenerator.exe"

Invoke-Command $licensegen "$inputtest" "$outputtest"

When I run this, I get the error:

Invoke-Command : A positional parameter cannot be found that accepts argument
'C:\Folder\Output.xml'.
At line:5 char:1
+ Invoke-Command $licengegen "$inputtest" "$outputtest"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Invoke-Command], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.InvokeCommandCommand

I have also tried running with Invoke-Expression but get the exact same error (except it says "Invoke-Expression" at the beginning). Anybody have any idea what I'm doing wrong here?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Christopher Cass
  • 817
  • 4
  • 19
  • 31
  • & $licensegen "$inputtest" "$outputtest" did the trick. Thank you all! – Christopher Cass May 16 '17 at 17:08
  • This question has already been discussed in detail here, and has some exceptionally good and unusual answers: https://stackoverflow.com/questions/1673967/how-to-run-an-exe-file-in-powershell-with-parameters-with-spaces-and-quotes – Binarus Dec 02 '20 at 13:19

3 Answers3

7

You're looking for the call operator (&):

& $licensegen "$inputtest" "$outputtest"

Invoke-Command is essentially for running scriptblocks on other hosts and/or in other user contexts.

Noelle L.
  • 100
  • 6
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
4

Start-Process is great because you can runas, redirect output, hide the child processes window and much more.

Start-Process -FilePath $licensegen -Argumentlist $inputtest,$outputtest

Community
  • 1
  • 1
Nick
  • 1,783
  • 1
  • 15
  • 18
2

& "[path] command" [arguments]

Just replace Invoke-Command with &

bryvo01
  • 121
  • 1
  • 4