gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 22:51:07 25 4
gpt4 key购买 nike

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

"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"



当包含在批处理文件中时,此命令可以正常执行,但是当我将该命令复制并粘贴到 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 }

我知道我可能对上面的代码有多个问题,即如何传递参数以及如何处理参数列表中的引号。虽然不正确,但它应该说明我正在努力实现的目标。关于从哪里开始的任何建议?

最佳答案

从 PowerShell 调用命令行应用程序可能非常棘手。几周前@Jaykul 写了很棒的博文 The problem with calling legacy/native apps from PowerShell他描述了人们在这种情况下会遇到的问题。当然也有解决方案;)

编辑 - 正确的网址

该文章不再可用,因此只能通过 web.archive.org 看到该文章 - 参见 cached article

关于powershell - 从 Powershell 调用带有非常长的可变参数列表的程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5505983/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com