Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

powershell need help executing command line program

Posted on 2013-12-27
3
Medium Priority
?
954 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 19

Accepted Solution

by:
Raheman M. Abdul earned 1000 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 85

Assisted Solution

by:oBdA
oBdA earned 1000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Suggested Courses

916 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