Unable to Launch Command Line Executable

by TheRealRobG at 2012-09-04 14:20:45

Hello,

I’m a newbie to PowerShell, though I have experience coding in PHP and VB.

I’ve got my script generating the commands I want to run as string variables OK, and they list on screen fine. If I cut & paste them into Windows 7 at the command line they work fine (held in a variable called $execcommand):

"C:\Program Files (x86)\Adobe\Adobe DNG Converter.exe" -d "C:\PhotoImport\3_DNG_From_Adobe_DNG_Converter" J:\S0016439.RAF

However, as soon as I stick ‘&’ in front of them to actually run them from within PowerShell I’m getting error messages (and this happens whether I use single or double quotes to surround the long file paths) - via &$execcommand:

The term ‘"C:\Program Files (x86)\Adobe\Adobe DNG Converter.exe" -d "C:\PhotoImport\3_DNG_From_Adobe_DNG_Converter" J:\S0016439.RAF’ is not recognized as the name of a cmd
let, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\PhotoImport\photoimport.ps1:74 char:18
+ & <<<< $execcommand
+ CategoryInfo : ObjectNotFound: ("C:\Program Fil...J:\S0016439.RAF:String) , CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException


Can anyone please suggest what I’m doing wrong?

Many thanks!
by willsteele at 2012-09-04 14:37:30
Instead of using & trying using the cmdlet Invoke-Expression with your variable. Additionally, the Start-Process cmdlet gives you a lot of control over how you can start processes. Additionally, the Start-Process cmdlet gives you the -Wait option, which is useful in case you need to stall processing at a given point until the task at hand completes.
by poshoholic at 2012-09-04 14:39:35
Hi there,

The error message is telling you that this is not a valid command:

‘"C:\Program Files (x86)\Adobe\Adobe DNG Converter.exe" -d "C:\PhotoImport\3_DNG_From_Adobe_DNG_Converter" J:\S0016439.RAF’

That’s your command string, but it isn’t the command path. Only the first part is your command path, and the rest are the command parameters. So, instead of doing this:

& '"C:\Program Files (x86)\Adobe\Adobe DNG Converter.exe" -d "C:\PhotoImport\3_DNG_From_Adobe_DNG_Converter" J]
You need to simply remove the external quotes and do this:

& "C:\Program Files (x86)\Adobe\Adobe DNG Converter.exe" -d "C:\PhotoImport\3_DNG_From_Adobe_DNG_Converter" J]
That will tell PowerShell to invoke the "Adobe DNG Converter.exe" program and pass in the parameters you have provided.
by TheRealRobG at 2012-09-04 15:14:43
[quote="poshoholic"]Hi there,

The error message is telling you that this is not a valid command:

‘"C:\Program Files (x86)\Adobe\Adobe DNG Converter.exe" -d "C:\PhotoImport\3_DNG_From_Adobe_DNG_Converter" J:\S0016439.RAF’

That’s your command string, but it isn’t the command path. Only the first part is your command path, and the rest are the command parameters. So, instead of doing this:

& '"C:\Program Files (x86)\Adobe\Adobe DNG Converter.exe" -d "C:\PhotoImport\3_DNG_From_Adobe_DNG_Converter" J]
You need to simply remove the external quotes and do this:

& "C:\Program Files (x86)\Adobe\Adobe DNG Converter.exe" -d "C:\PhotoImport\3_DNG_From_Adobe_DNG_Converter" J]
That will tell PowerShell to invoke the "Adobe DNG Converter.exe" program and pass in the parameters you have provided.[/quote]

Many thanks for the response.

The outer single quotes were added by PowerShell debugger in the error message. They aren’t in the command I’m trying to run. You can see the actual command as held in the variable listed above. There are no single quotes around it:

"C:\Program Files (x86)\Adobe\Adobe DNG Converter.exe" -d "C:\PhotoImport\3_DNG_From_Adobe_DNG_Converter" J:\S0016439.RAF

Maybe I’m misunderstanding you?
by poshoholic at 2012-09-04 15:23:01
Got it, sorry, that was my misunderstanding. I didn’t notice the important detail about you using $execCommand. In that case you could use Invoke-Expression as Will mentioned, or you could do this:

$ExecutionContext.InvokeCommand.NewScriptBlock("& $execCommand").Invoke()
by TheRealRobG at 2012-09-04 15:28:05
Many thanks all.

In the end, I settled on the Start-Process command which seems to work very well as long as you separate the executable from the arguments thusly:

Start-Process $dngexeccommand $currentdngexecargs -wait

Variable values here:

$dngexeccommand: "C:\Program Files (x86)\Adobe\Adobe DNG Converter.exe"

$currentdngexecargs: -d "C:\PhotoImport\3_DNG_From_Adobe_DNG_Converter" J:\Test01\S0000000.RAF

Hope this helps someone in the same position!