从Powershell调用一个带有非常长的可变参数列表的程序?

Nat*_*ate 4 powershell batch-file

我目前正在尝试将一系列批处理文件转换为PowerShell脚本.我想以递归方式为目录中存在的源文件运行编译器.编译器需要一长串参数.问题是,我希望参数是可变的,所以我可以根据需要更改它们.这是批处理文件中的典型调用(为了可读性和长度而简化):

"C:\ PICC编译器\ 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"

当包含在批处理文件中时,此命令执行正常,但是当我将命令复制并粘贴到PowerShell中时,我开始收到错误.这只是我第二天使用PowerShell,但我过去使用.NET开发.我设法凑合了以下尝试:

$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 }
Run Code Online (Sandbox Code Playgroud)

我知道上面的代码可能有多个问题,即如何传递参数以及我如何处理参数列表中的引号.这是不正确的,它应该说明我想要实现的目标.有关从哪里开始的任何建议?

ste*_*tej 6

从PowerShell调用命令行应用程序可能非常棘手.几个星期前,@ Jaykul编写了一篇很棒的博客文章,从PowerShell调用遗留/本机应用程序的问题,他描述了人们在这种情况下会遇到的问题.当然也有解决方案;)

编辑 - 更正网址

这篇文章不再可用,因此只能通过web.archive.org查看 - 请参阅缓存文章