我目前正在尝试将一系列批处理文件转换为 powershell 脚本。我想递归地为目录中存在的源文件运行编译器。编译器需要一长串参数。问题是,我希望参数是可变的,这样我就可以根据需要更改它们。这是批处理文件中的典型调用(为了可读性和长度而进行了简化):
“C:\PICC 编译器\picc18.exe”--pass1
“C:\Src 文件\somefile.c”
“-IC:\包含文件” “-IC:\标头
文件”-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=错误 [ %n] %f;%l.%c
%s" "--msgformat=咨询[%n] %s" --OBJDIR="C:\Built Files"
“--warnformat=警告 [%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 }
我知道上面的代码可能有多个问题,即如何传递参数以及如何处理参数列表中的引号。尽管它是不正确的,但它应该说明我想要实现的目标。关于从哪里开始有什么建议吗?