簡體   English   中英

從Powershell調用一個帶有非常長的可變參數列表的程序?

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

我目前正在嘗試將一系列批處理文件轉換為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 }

我知道上面的代碼可能有多個問題,即如何傳遞參數以及我如何處理參數列表中的引號。 這是不正確的,它應該說明我想要實現的目標。 有關從哪里開始的任何建議?

從PowerShell調用命令行應用程序可能非常棘手。 幾個星期前,@ Jaykul編寫了一篇很棒的博客文章,從PowerShell調用遺留/本機應用程序的問題 ,他描述了人們在這種情況下會遇到的問題。 當然也有解決方案;)

編輯 - 更正網址

這篇文章不再可用,因此只能通過web.archive.org查看 - 請參閱緩存文章

使$arglist成為一個數組而不是一個字符串。 單個字符串將始終作為單個參數傳遞,這是您想要的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM