Solved

powershell need help executing command line program

Posted on 2013-12-27
3
869 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 19

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 84

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
This video Micro Tutorial is the second in a two-part series that shows how to create and use custom scanning profiles in Nuance's PaperPort 14.5 (http://www.experts-exchange.com/articles/17490/). But the ability to create custom scanning profiles a…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

730 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