0

I am trying to pass a .txt file with arguments to an .exe file via powershell. Currently, this is what I have.

Write-Host "starting upgrade at $(Get-Date -format 'U')"


C:\dev\temp.exe.exe /DIR="C:\TEST" /BTPServerHost="Test" /DBInstance="testDB" /Log=C:\path\to\test\testlog.txt

This is calling a function within an InnoScript file that accepts command line input.

How would I format the .txt file, and how would I be able to pass it into the .exe? Any help would be appreciated! Thanks!

Stacker
  • 137
  • 3
  • 12

2 Answers2

1

If you are saying, in this text file, there are just these argument line on individual rows and you are saying you've already tried something like the below and were not successful?

You also don't need the Write-Host for the message line, since the default is output to screen. You normal only need Write-Host for colorizing screen text, and a few other formatting cases, depending on what you are doing. All-in-All, Write-Host should be avoided.

"starting upgrade at $(Get-Date -format 'U')"
($ConsoleCommand = Get-Content -Path 'd:\temp\input.txt' -Raw)

# Results - showing the commands in the file before process them

whoami
get-date
'hello world'

Without using the -Wait switch, this will spawn 3 separate PowerShell consoles with the results

ForEach($CmdLine in $ConsoleCommand)
{ Start-Process -FilePath powershell -ArgumentList "-NoExit","-Command  &{ $CmdLine }" }

you can of course point to your .exe vs what I am doing here.

Start-Process

By adding the -Raw after specifying the .txt file path it ignores newline characters and returns the entire contents of a file in one string with the newlines preserved. By default, newline characters in a file are used as delimiters to separate the input into an array of strings.

Stacker
  • 137
  • 3
  • 12
postanote
  • 15,138
  • 2
  • 14
  • 25
  • Thank you for your response. When running this command with the .exe, it brings a new instance of the installer per argument in the text file. So I am getting multiple installation wizard windows opening each time I run the script. Any idea as to run only one instance of the executable? – Stacker Oct 19 '18 at 14:45
  • And you should since the command are provided as a list. If each of what is in your list is a string of argument to the exe, then they should all be one line, so al are passed on a single call. – postanote Oct 19 '18 at 16:43
0

This script takes parameters from a txt file and passes them into an executable and auto-populates the fields in the installation wizard. This is what I'm looking to do, but I don't want to start a new process for each argument in the input txt file.

Write-Host "starting upgrade at $(Get-Date -format 'U')" Get-Content -Path C:\TestInput.txt -Raw | foreach {Start-Process C:\dev\test.exe -ArgumentList $_}

The TestInput.txt file passed in looks like this:

 /DIR="C:\TEST" 
 /ServerHost="Test" 
 /DBInstance="testDB" 
 /Log=C:\testlog.txt
Stacker
  • 137
  • 3
  • 12
  • 1
    How is this an answer? How does it solve the problem you describe in the question above? – Ken White Oct 18 '18 at 17:49
  • This script takes parameters from a txt file and passes them into an executable and auto-populates the fields in the installation wizard. It works, but I don't want to start a new process for each argument in the input txt file – Stacker Oct 18 '18 at 17:56
  • Then edit your answer and explain how it solves the problem, instead of doing so in a comment. A code dump with no explanation (especially when it is very similar to the problem code in the question) does not appear to be an answer. You should explain what the change was and why or how it solves the original problem. (You should also not add a new problem description to an answer, because it's a new problem and not an answer.) – Ken White Oct 18 '18 at 17:58