Solved

powershell need help executing command line program

Posted on 2013-12-27
3
833 Views
Last Modified: 2013-12-30
I think it has to do with single and double quotes. I had the same problem using tiffcp.exe and it took me several days of trial and error to get it working. I was not able to get PDFtk working with variables:
What do you think?
thanks!


$input = 'one.PDF two.PDF'
$options = ' cat output '
$output = ' c:\temp\document.PDF'



#this works:
& pdftk.exe -version

#this does work:
&pdftk.exe one.PDF two.PDF cat output "c:\temp\document.PDF"


#this doesn't work:
& pdftk.exe $input cat output $output
0
Comment
Question by:johnj_01201
3 Comments
 
LVL 18

Accepted Solution

by:
Raheman M. Abdul earned 250 total points
Comment Utility
$input = 'one.PDF two.PDF'
$options = ' cat output '
$output = ' c:\temp\document.PDF'

$program = "pdftk.exe"
$arguments = "$input cat output $output"
Start-Process -FilePath "$program" -ArgumentList "$arguments"
0
 
LVL 82

Assisted Solution

by:oBdA
oBdA earned 250 total points
Comment Utility
This has nothing to do with single and double quotes, but with how Powershell treats arguments. When it parses a command line where you pass variables, and these variables contain spaces, it automatically adds quotes, so if $input is 'one.PDF two.PDF', and you pass that to pdftk.exe, the resulting command line will be pdftk.exe "one.PDF two.PDF".
To run external programs with dynamically built arguments, it's easiest to put the arguments into an array:
$CommandLine = @()
$CommandLine += "one.PDF"
$CommandLine += "two.PDF"
$CommandLine += "cat"
$CommandLine += "output"
$CommandLine += "c:\temp\document.PDF"
& pdftk.exe $CommandLine

Open in new window

One of the best articles explaining this behavior I've come across so far is this one:
PowerShell and external commands done right
http://edgylogic.com/blog/powershell-and-external-commands-done-right/
0
 
LVL 1

Author Closing Comment

by:johnj_01201
Comment Utility
Start-Process simply worked and solved the issue without needing to know how PS works internally. The $CommandLine method did not work and caused a giant sized error message containing dozens of XML errors and I do not know what it meant or where it came from. The points were divided because oBda's link provides great details on how PS runs command line programs and is something I really need to learn when I have more time. It also explains why it took me 3 days to get the previous command line program to work by messing with the double quotes and why the error message always shows a double quote (or two single quotes?) at the end of line that I could not figure out where in my code the extra quote was coming from. THANKS!
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article shows how to convert a multi-page PDF file into multiple image files, with one image file created for each page of the PDF. It does this by utilizing an excellent, free software package called GraphicsMagick. The solution is amazingly s…
This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
The viewer will learn how to count occurrences of each item in an array.
Sometimes we receive PDF files that are in the wrong orientation. They may be sideways or even upside down. This most commonly happens with scanned or faxed documents. It is possible to rotate the view of these PDFs with the free Adobe Reader produc…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now