简体   繁体   中英

Executing an exe with arguments using Powershell

This is what I want to execute:

c:\\Program Files (x86)\\SEQUEL ViewPoint\\viewpoint.exe /Setvar((POSTSTR $POSTSTR)(POSTEND $POSTEND)) /G:C:\\viewpointfile.vpt /D:C:($BEGDATE to $TODDATE).xls

This is what I have tried:

$a = "/Setvar((POSTSTR $POSTSTR)(POSTEND $POSTEND))"

$b = "/G:C:\\viewpointfile.vpt"

$c = "/D:C:($BEGDATE to $TODDATE).xls"

$Viewpoint = "c:\\Program Files (x86)\\SEQUEL ViewPoint\\viewpoint.exe"

&$Viewpoint $a $b $c

When I execute this I receive an error stating:

File C:\\viewpointfile.vpt "/D:C:($BEGDATE to $TODDATE).xls" not found!

I'm not sure where it gets the extra quotes from. If I run the command with just $a and $b it runs fine.

Any help would be greatly appreciated. Thanks! :)

Update

manojlds suggested echoargs so here it the output from it:

&./echoargs.exe $viewpoint $a $b $c

Arg 0 is C:\\Program Files (x86)\\SEQUEL ViewPoint\\viewpoint.exe

Arg 1 is /Setvar((POSTSTR 20101123)(POSTEND 20111123))

Arg 2 is /G:C:\\viewpointfile.vpt

Arg 3 is /D:C:(2010-11-23 to 2011-11-23 PM).xls

It appears that all the arguments are being passed properly. When I run this as a command in cmd.exe it executes perfectly. So something on Powershells end must be messing up the output.

Is there any other way to go about executing this command using Powershell?

Get echoargs.exe from Powershell community extension ( http://pscx.codeplex.com/ ) to figure out the arguments that Powershell sends to your exe.

$a = "/Setvar((POSTSTR $POSTSTR)(POSTEND $POSTEND))"
$b = "/G:C:\viewpointfile.vpt"
$c = "/D:C:($BEGDATE to $TODDATE).xls"
$echoArgs = ".\echoargs.exe"
&$echoArgs $a $b $c

You seem to be passing the arguments fine however, but the viewpoint.exe seems to be acting up. I don't see what you are doing here:

$c = "/D:C:($BEGDATE to $TODDATE).xls"

After C: there is no \\ and also your error message that you have pasted shows $BEGDATE and $TODDATE verbatim, which is not possible as they would have been substituted with their values.

I've found the method blogged by Joel Bennett to be the most reliable when calling legacy commands

http://huddledmasses.org/the-problem-with-calling-legacy-or-native-apps-from-powershell/

I've had to use this when calling LogParser from Powershell:

set-alias logparser "C:\Program Files (x86)\Log Parser 2.2\LogParser.exe"
start-process -NoNewWindow -FilePath logparser -ArgumentList @"
"SELECT * INTO diskspaceLP FROM C:\Users\Public\diskspace.csv" -i:CSV -o:SQL -server:"Win7boot\sql1" -database:hsg -driver:"SQL Server" -createTable:ON
"@

If I can't run a command like this it usually works for me with Invoke-Expression. Can't test yours though.

Invoke-Expression "$viewpoint $a $b $c"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM