Al Phaba
Al Phaba

Reputation: 6785

How to call an exe from powershell with parameters?

I need to call a executeable within my powershell script and want to wait for its result. To this exe I need to give some parameters which are contained in my variables, but this does not work

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

I try to call the gswin32c.exe like this

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

But this results in the error

    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

I think there can be an errror because I need to put the parameters in quotes, but what are the correct ones? My tests did not work.

thanks

Upvotes: 2

Views: 4225

Answers (2)

Bill_Stewart
Bill_Stewart

Reputation: 24585

You should be able to write your command this way:

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

I wrote an article about this that may be helpful:

Windows IT Pro: Running Executables in PowerShell

The showargs.exe utility provided in the article's download that can be helpful in troubleshooting PowerShell's command-line parsing.

Upvotes: 1

henrycarteruk
henrycarteruk

Reputation: 13227

Start-Process with -Wait should do you:

$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

Upvotes: 1

Related Questions