launching an application with a PS script

Hi everybody,

          I'm an absolute beginner with PowerShell, so please be patient :-)
I'm going to write my first script for PowerShell. The script core concerns an external software I need to launch. This is a typical code for a batch file:

cd case_path
echo "macro" | "app_path"app.exe case.ext 2>&1 >> log

It results in:

- go to the folder containing the 'case' to launch (just a file containing settings)
- run the case with application.exe, execute the 'internal macro' and generate a log file

I'm able to run the application with

Invoke-Item app_path\app.exe

but I can't run the specific case (and the internal macro too). Could you help me, the batch script as a starting point?
January 27th, 2015 12:39pm

Hi Vaina,

you can use the Start-Process cmdlet to run applications:

Start-Process "Apppath\app.exe" -ArgumentList "case.ext" 2>&1 | Out-File "file.log"

Cheers,
Fred

Free Windows Admin Tool Kit Click here and download it now
January 27th, 2015 12:45pm

Use it like this to input the macro.  Start by placing the macro into a file called macro.inp

$props=@{
   FilePath='app_path\app.exe'
   RedirectStandardInput='c:\macro.inp'
   WorkingDirectory='case-path'
   NoNewWindow=$true
}
Start-Process @props 2>&1 >> log.txt
January 27th, 2015 1:07pm

Method #2

cmd /c "cd case_path && echo macro | app_path\app.exe case.ext 2>&1 >> log"

Free Windows Admin Tool Kit Click here and download it now
January 27th, 2015 1:14pm

Hi Fred,

     thanks for your help. I was in doubt about Invoke-item and Start-Process and you probably showed the right direction. Focusing on the application launching (without log generation), I made a test:

$FS_path = $env:fsinstall
Start-Process "$FS_path\bin\win32\xfsflow.exe" -ArgumentList ".\f1\doubleSkegCase.fme"

xfsflow starts and the .fme file (above 'case') is loaded, but I don't know how to call the macro. The macro is a set of commands inside the case file (just a text file), between specific markers. I usually call the macro with the command line in the first post.

January 27th, 2015 3:17pm

I posted an exaple of how to call the macro file.

Free Windows Admin Tool Kit Click here and download it now
January 27th, 2015 3:21pm

Hi jrv,

     thanks for your attention. Method #2 works , but I'm looking for a more elegant and 'PowerShell' solution. This is a chance to learn this surprising scripting language. So I'm interested in method #1. Focusing on application launching, you set a structure/class for the Start-Process cmdlet (or is that an associative array?). I guess that Start-Process expects that kind of object (are FilePath and so on defult entries?). Anyway, maybe the application I need is not compatible with that.

I explain how the above application works. The code expects a simple text file with all settings, values and other stuff to run. If launched, a cmd window opens and wait for a command. For instance, if I write 'GUI' an interface opens and the case file is more 'readable' and I can edit and so on. The case file also includes a set of commands (with control structures too), just a few lines between specific markers (and the macro name). So I can call the macro at the application prompt in the cmd window. That's the meaning of

echo "macro" | "app_path"app.exe case.ext

January 27th, 2015 3:36pm

Method # 2 IS PowerShell.

Method #1 is how too use Start-Process with a macro file.

$props=@{
   FilePath='app_path\app.exe'
   RedirectStandardInput='c:\macro.inp'
   WorkingDirectory='case-path'
   NoNewWindow=$true
}
Start-Process @props 2>&1 >> log.txt
Free Windows Admin Tool Kit Click here and download it now
January 27th, 2015 3:41pm

OK, method #1 is PS and it's enough for me :-)
Anyway, I'm interested in the object you defined. As I wrote, I'd like to get deeper inside PS. I can't find similar samples in the guides I downloaded, I'm 'googling' Start-Process but I don't use the proper keywords and find only 'one-line' samples: Start-Process cmdlet and all standard parameters (e.g  -ArgumentList or -Verb). Thanks anyway.

Update: I just found out there are two different sets of parameters for Start-Process. Now the array described above is more clear: it's a hash table!. I'm going to test again the code.

  • Edited by vaina74 Tuesday, January 27, 2015 2:02 PM
January 27th, 2015 4:46pm

OK, I'm here again, just for feedback and sharing (are there other newbies out there?)
I made some tests with both methods (thanks Fred and jrv!).

method cmd /c

$FS_path = $env:fsinstall + '\bin\win32\'
$case_path = '.\f1'
$cmdString = 'cd ' + $case_path + ' && echo "res" | "' + $FS_path + '"xfsflow doubleSkegCase.fme 2>&1 >> log.txt'


method Start-Process

$FS_path = $env:fsinstall + '\bin\win32\xfsflow'
$args = '-c "res" doubleSkegCase.fme'
$FS_case = '.\f1'
$sim=@{
		FilePath = $FS_path
		ArgumentList = $args
		WorkingDirectory = $FS_case
		RedirectStandardOutput = "$FS_case\log.txt"
}
Start-Process @sim -noNewWindow -wait

just two more questions:

1. is it possible to get rid of the standard error output? I'd like to have no error messages in the terminal or as a file

2. I'd like to make the macro "res" in the ArgumentList as a variable, but I'm not able to. I guess the problem concerns escape characters or similar, could you help me?


  • Edited by vaina74 Wednesday, January 28, 2015 2:34 PM
January 28th, 2015 12:25pm

Hi Vaina,

sorry for the late response.

1.) You can probably do this by redirecting Standard Error (-RedirectStandardError)

2.) That is due to the single quotes - they prevent Powershell from interpreting string content. Do This:

# Old String
' && echo "res" | "'

# New String
' && echo "' + $res + '" | "'

Cheers,
Fred

Free Windows Admin Tool Kit Click here and download it now
February 24th, 2015 4:46am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics