Call a program from Powershell w/ very long, variable argument list?

Question:

I am currently trying to convert a series of batch files to powershell scripts. I would like to run a compiler for the source files that exist in a directory, recursively. The compiler requires a long list of arguments. The catch is, I want the arguments to be variable so I can change them as needed. This is a typical call from the batch file (simplified for readability and length):

“C:\PICC Compilers\picc18.exe” –pass1
“C:\Src Files\somefile.c”
“-IC:\Include Files” “-IC:\Header
Files” -P
–runtime=default,+clear,+init,-keep,+download,+stackwarn,-config,+clib,-plib
–opt=default,+asm,-speed,+space,9 –warn=0 –debugger=realice -Blarge –double=24 –cp=16 -g –asmlist “–errformat=Error [%n] %f; %l.%c
%s” “–msgformat=Advisory[%n] %s” –OBJDIR=”C:\Built Files”
“–warnformat=Warning [%n] %f; %l.%c %s”

This command executes fine when included in a batch file, but I start getting errors when I copy and paste the command into powershell. This is only my second day working with powershell, but I have developed with .NET in the past. I have managed to scrape together the following attempt:

I know that I probably have multiple issues with the above code, namely how to pass arguments and how I am dealing with the quotes in the argument list. Incorrect as it is, it should illustrate what I am trying to achieve. Any suggestions on where to start?

Answer:

Calling command line applications from PowerShell might be really tricky. Several weeks ago @Jaykul wrote great blog post The problem with calling legacy/native apps from PowerShell where he describes gotchas which people will meet in this situations. And there is of course solution too 😉

edit – correct url

The article is no more available, so it’s only possible to see that through web.archive.org – see cached article

Source:

Call a program from Powershell w/ very long, variable argument list? by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply