Call a program from a Powershell list with a very long list of variable arguments?

I am currently trying to convert a series of batch files into powershell scripts. I would like to run the compiler for the source files that exist in the directory, recursively. The compiler requires a long list of arguments. Trap, I want the arguments to be variables, so I can change them as needed. This is a typical call from a 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, + prim, -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 runs fine if it is included in the batch file, but I'm starting to get errors when copying and pasting the command in powershell. This is only my second day working with powershell, but I have developed this with .NET in the past. I managed to make the following attempt:

$srcFiles = Get-ChildItem . -Recurse -Include "*.c" $srcFiles | % { $argList = "--pass1 " + $_.FullName; $argList += "-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' '--warnformat=Warning [%n] %f; %l.%c %s" $argList += "--OBJDIR=" + $_.DirectoryName; &"C:\PICC Compilers\picc18.exe" $argList } 

I know that I probably have several problems with the above code, namely how to pass arguments and how I deal with quotes in the argument list. Wrong, it is, it should illustrate what I am trying to achieve. Any suggestions on where to start?

+4
source share
2 answers

Calling command line applications from PowerShell can be very difficult. A few weeks ago @Jaykul wrote a wonderful blog post. The problem with calling outdated / native applications from PowerShell , where he describes getchas that people will encounter in these situations, And, of course, a solution too;)

edit - correct url

The article is not available, so you can see that through web.archive.org - see the cached article

+6
source

Make a $arglist array instead of a string. A single line will always be passed as the only argument that you do not need here.

+4
source

Source: https://habr.com/ru/post/1346123/


All Articles