Solved

powershell need help executing command line program

Posted on 2013-12-27
3
843 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
ID: 39742530
$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 83

Assisted Solution

by:oBdA
oBdA earned 250 total points
ID: 39742542
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
ID: 39746271
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

911 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

17 Experts available now in Live!

Get 1:1 Help Now