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
Solved

powershell need help executing command line program

Posted on 2013-12-27
3
863 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 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone 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

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 …
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
Microsoft Office Picture Manager has a Picture Shortcuts pane that shows a list with the Recently Browsed folders. While creating my video Micro Tutorial here at Experts Exchange showing How to Install Microsoft Office Picture Manager in Office 2013…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

808 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