0

I have a working, simple batch file cmd:

D:\Software\MySoftware.exe --process MyConfig.tuc

When I try to replicate in Powershell using either Invoke-Expression or the call operator '&', the code executes but the log indicates it has not picked up the switch: --process MyConfig.tuc

Invoke-Expression "D:\Software\MySoftware.exe --process MyConfig.tuc"

Or

& D:\Software\MySoftware.exe --process MyConfig.tuc

Edit: I've discovered that if you pass the full path of the .tuc file, the command is successful.

Invoke-Expression "D:\Software\MySoftware.exe --process D:\Software\MyConfig.tuc"
felixmc
  • 516
  • 1
  • 4
  • 19
  • 1
    I have no idea what `Invoke-Expression` has to do with your task. Have you tried, `D:\Software\MySoftware.exe --process MyConfig.tuc`? If your actual path includes spaces, you may find that `& D:\Software\MySoftware.exe --process MyConfig.tuc` works better for you. If you want to use a command, then `Start-Process` may help you, `Start-Process -FilePath D:\Software\MySoftware.exe -ArgumentList "--process MyConfig.tuc"`. – Compo Apr 21 '20 at 19:20
  • 1
    Obviously if your executable isn't searching a known or relative path for the file in the argument, you'll need to provide that location or change your working directory as needed. `Start-Process` has a `-WorkingDirectory` option for that too. – Compo Apr 21 '20 at 19:26
  • 1
    Instead of adding the solution to the question you should better post it as an answer... – aschipfl Apr 22 '20 at 19:30
  • @Compo you are correct. If you wish to post as answer, I will accept. This works: Start-Process -FilePath D:\Software\MySoftware.exe -WorkingDirectory D:\Software -ArgumentList "--process MyConfig.tuc" – felixmc Apr 23 '20 at 10:48

1 Answers1

0

My question asked why Invoke-Expression needed the full path to the config file when the batch file did not. As stated in comments by @compo this is likely due differences in how PS or cmd work in regard to relative paths.

@compo made the more pertinent observation that Start-Process would be a better solution, which I found it was. Not only does it work, it has switches well suited to my needs, notably -wait which Invoke-expression was not natively doing (I couldn't confirm if it could wait, there are suggestions online it can).

Start-Process -FilePath D:\Software\MySoftware.exe -WorkingDirectory D:\Software  -ArgumentList "--process MyConfig.tuc" -wait
felixmc
  • 516
  • 1
  • 4
  • 19