Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - executing applications with parameter

Reply
 
Old 11-07-2006   #1 (permalink)
DJHolliday


 
 

executing applications with parameter

Hi,

Sorry if this is duplicate, but i could not find anything useful so
far.

In my profile I set several environment variables

$env:EXT_VW=c:\dev\vw

Now I want to use that to start the application

&"$env:EXT_VW\vw.exe"

works fine, but how can I pass a parameter string?

the final command should look like
c:\dev\vw\vw.exe c:\dev\vw\filelist.lst -lw32\ost.txt

(The last parameter should be relative to the current directory)

I'm a real beginner in the PowerShell and it would be nice, if somebody
could point me in the right direction.

Thanks,

DJ


My System SpecsSystem Spec
Old 11-07-2006   #2 (permalink)
Maximilian Hänel


 
 

Re: executing applications with parameter

Hi DJHolliday,

> the final command should look like
> c:\dev\vw\vw.exe c:\dev\vw\filelist.lst -lw32\ost.txt


This could be done like this:

$env:AppPath="C:\windows"
invoke-expression "$Env:AppPath\notepad.exe test.txt"

> (The last parameter should be relative to the current directory)


It depends of whether your app knows how to resolve relative paths or
not. In general you better resolve that path before passing it to your
legacy app. If the file already exists then you can resolve it with the
Convert-Path cmdlet. If the file does not exist you should read the
thread "Resolve-path on non existing file" in this newsgroup.

hth

Max
My System SpecsSystem Spec
Old 11-07-2006   #3 (permalink)
AndreasH


 
 

Re: executing applications with parameter

It worked fine before in the regular cmd.exe
I just want to evaluate the PowerShell and how difficult it is to
rewrite our commands.

The funny thing is, the parameter gets passed in correct, but
CreateFile() returns file does not exist. As I said, it worked fine
using cmd.exe

Any ideas why?

Maximilian Hänel schrieb:

> Hi DJHolliday,
>
> > the final command should look like
> > c:\dev\vw\vw.exe c:\dev\vw\filelist.lst -lw32\ost.txt

>
> This could be done like this:
>
> $env:AppPath="C:\windows"
> invoke-expression "$Env:AppPath\notepad.exe test.txt"
>
> > (The last parameter should be relative to the current directory)

>
> It depends of whether your app knows how to resolve relative paths or
> not. In general you better resolve that path before passing it to your
> legacy app. If the file already exists then you can resolve it with the
> Convert-Path cmdlet. If the file does not exist you should read the
> thread "Resolve-path on non existing file" in this newsgroup.
>
> hth
>
> Max


My System SpecsSystem Spec
Old 11-07-2006   #4 (permalink)
Maximilian Hänel


 
 

Re: executing applications with parameter

Hi Andreas

> The funny thing is, the parameter gets passed in correct, but
> CreateFile() returns file does not exist. As I said, it worked fine
> using cmd.exe


Try something like this:

$env:AppPath="C:\windows"
$file=Convert-Path test.txt
invoke-expression "$Env:AppPath\notepad.exe $file"

Does it work now?

hth

Max
My System SpecsSystem Spec
Old 11-08-2006   #5 (permalink)
AndreasH


 
 

Re: executing applications with parameter

Unfortunately not.
When I do the following

$file = convert-Path w32\ost.txt
invoke-expression ".\vw.exe -l$file"

the final string looks like
".\vw.exe -lc: \dev\vw\w32\ost.txt"

notice the space between drive and directory?
is there another parameter to eliminate the space?

Thanks

Maximilian Hänel schrieb:

> Hi Andreas
>
> > The funny thing is, the parameter gets passed in correct, but
> > CreateFile() returns file does not exist. As I said, it worked fine
> > using cmd.exe

>
> Try something like this:
>
> $env:AppPath="C:\windows"
> $file=Convert-Path test.txt
> invoke-expression "$Env:AppPath\notepad.exe $file"
>
> Does it work now?
>
> hth
>
> Max


My System SpecsSystem Spec
Old 11-08-2006   #6 (permalink)
AndreasH


 
 

Re: executing applications with parameter

Oops, sorry, the string looks good without the blank
but when the command line inside the application looks weird.
I guess I have to check our application first.

thanks a lot for your help.

My System SpecsSystem Spec
Old 11-08-2006   #7 (permalink)
AndreasH


 
 

Re: executing applications with parameter

Weird, if I use

$file = convert-path w32\ost.txt
invoke-expression ".\vw.exe -l$file"

I get the following command line passed in
"-lc: \dev\vw\w32\ost.txt"

if I put a space between -l and the variable, like

invoke-expression ".\vw.exe -l $file

I get

"-l c:\dev\vw\w32\ost.txt"

the difference is just one space here, which corrupts the whole
process.
Any ideas, why this happens?

My System SpecsSystem Spec
Old 11-08-2006   #8 (permalink)
Maximilian Hänel


 
 

Re: executing applications with parameter

Hi Andreas

> Weird, if I use
>
> $file = convert-path w32\ost.txt
> invoke-expression ".\vw.exe -l$file"
>
> I get the following command line passed in
> "-lc: \dev\vw\w32\ost.txt"
>
> if I put a space between -l and the variable, like
>
> invoke-expression ".\vw.exe -l $file
>
> I get
>
> "-l c:\dev\vw\w32\ost.txt"


That's really weird! Don't ask me what's going on here.


If one wants to reproduce (and/or confirm) that behavior create a file
named DumpArgs.cs with the following content:

//DumpArgs.cs
using System;

static class Program
{
static void Main()
{
Console.WriteLine(Environment.CommandLine);
}
}
// EOF

Next compile the file:

$compilerPath="$env:SystemRoot\Microsoft.NET\Framework\v2.0.50727\csc.exe"
invoke-expression "$compilerPath DumpArgs.cs"

To reproduce that "strange space behavior"(TM) we can now execute the
following lines:

$file = "c:\Windows\notepad.exe"
invoke-expression ".\DumpArgs.exe -l$file"

The output is:
"H:\User\Max\Eigene Dateien\WindowsPowerShell\DumpArgs.exe" -lc:
\Windows\notepad.exe

(Note: There's an unwanted space between the drive and directory part!)

But even if we "hardcode" the file name:
invoke-expression ".\DumpArgs.exe -lc:\Windows\notepad.exe"

We get the same strange result...

if one the other hand we put a space between the -l switch and the path
then everything works as expected:

invoke-expression ".\DumpArgs.exe -l c:\Windows\notepad.exe"

Output:

"H:\User\Max\Eigene Dateien\WindowsPowerShell\DumpArgs.exe" -l
c:\Windows\notepad.exe

Strange...

cu

Max
My System SpecsSystem Spec
Old 11-08-2006   #9 (permalink)
AndreasH


 
 

Re: executing applications with parameter


Max,

thanks for your help here.
Your example makes it very easy to reproduce.

Is there anybody from Microsoft, who could comment on that?

My System SpecsSystem Spec
Old 11-08-2006   #10 (permalink)
Maximilian Hänel


 
 

Re: executing applications with parameter

Hi Andreas

> thanks for your help here.


You're welcome!

> Your example makes it very easy to reproduce.
>
> Is there anybody from Microsoft, who could comment on that?


Yepp, that would be nice.

cu

Max
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Using multiple Parameter sets for a Parameter PowerShell
Several applications are trying to access the internet to specific ip address 10.0.0.138 & 224.0.0.2, while ARE NOT internet applications! ref/eDN3072784783 Vista General
executing another app PowerShell
How to best control parameter attributes and parameter parsing in your own scripts? PowerShell
Executing from \\127.0.0.1\C$ doesn't work PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46