如何使用参数调用PowerShell的exe?

时间:2016-12-14 16:02:18

标签: powershell

我需要在我的powershell脚本中调用可执行文件,并希望等待其结果。对于这个exe我需要提供一些包含在我的变量中的参数,但这不起作用

$gs = $currentPath +"\gs\gs8.70\bin\gswin32c.exe";
$outPdf="$cachepath\foobar_$IID.pdf"
$PS_FILE = "$cachepath\$IID.pdf"

我尝试像这样调用gswin32c.exe

& $gs q -dSAFER -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=$outPdf $PS_FILE

但这会导致错误

    gswin32c.exe : GPL Ghostscript 8.70: Unrecoverable error, exit code 1
    In F:\pdix-services\GWT-BPS\EventHandler\adobe-printing.ps1:21 Zeichen:1
    + & $gs q -dSAFER -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=$outPdf $PS_FIL ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (GPL Ghostscript...or, exit code 1:String) [], RemoteException
        + FullyQualifiedErrorId : NativeCommandError

我认为可能存在错误,因为我需要将参数放在引号中,但正确的是什么?我的测试不起作用。

感谢

2 个答案:

答案 0 :(得分:1)

您应该能够以这种方式编写命令:

& $gs q "-dSAFER" "-dNOPAUSE" "-dBATCH" "-sDEVICE=pdfwrite" "-sOutputFile=$outPDF"

我写了一篇关于此的文章可能会有所帮助:

Windows IT Pro: Running Executables in PowerShell

文章下载中提供的showargs.exe实用程序可以帮助您解决PowerShell的命令行解析问题。

答案 1 :(得分:0)

Start-Process的{​​p> -Wait应该为您做好准备:

$gs = "$currentPath\gs\gs8.70\bin\gswin32c.exe"
$arguments = "q -dSAFER -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=`"$cachepath\foobar_$IID.pdf`" `"$cachepath\$IID.pdf`""
Start-Process $gs $arguments -Wait
相关问题