2

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

Bill_Stewart
  • 22,916
  • 4
  • 51
  • 62
Al Phaba
  • 6,545
  • 12
  • 51
  • 83
  • This appears to be answered by http://stackoverflow.com/questions/12478535/how-can-i-execute-an-external-program-with-parameters-in-powershell – Jeff Zeitlin Dec 14 '16 at 16:27
  • 1
    What does `Write-Host "$gs q -dSAFER -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=$outPdf $PS_FILE"` give you? Does the output look like a reasonable command line? Do you need to add quotes around your parameters to escape spaces? – Jeroen Mostert Dec 14 '16 at 16:27
  • @JeffZeitlin This question also requires "wait for its result" which that does not. – henrycarteruk Dec 14 '16 at 16:30
  • @JamesC. if your assertion is that PowerShell won't wait for `gswin32c` to produce its output, that's just not true. It wouldn't be much of a shell if it did that. `Start-Process -Wait` only adds something for applications that don't have console I/O (like, say, Notepad) and you really do need to wait for the process itself to terminate. (Or you need one of the other options that `Start-Process` gives you, of course.) – Jeroen Mostert Dec 14 '16 at 16:36

2 Answers2

1

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
henrycarteruk
  • 12,708
  • 2
  • 36
  • 40
1

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.

Bill_Stewart
  • 22,916
  • 4
  • 51
  • 62