Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Problems running an app from within a script

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 01-28-2008   #1 (permalink)
Aether
Guest


 

Problems running an app from within a script


I have a simple and basic question about running external apps from a
script. If, within a script, I have this:


-----------------------------------------

$Path = "C:\Temp"

TestApp.exe "$Path\File.txt"

-----------------------------------------

...then it works fine.



However, if I have this:



-----------------------------------------

$TextEditor = "TestApp.exe"
$Path = "C:\Temp"

$TextEditor + " " + "`"$Path\File.txt`""

-----------------------------------------

...what happens is that the line is displayed like a text string, but
never actually executed.



What am I doing wrong?



Steve



My System SpecsSystem Spec
Old 01-28-2008   #2 (permalink)
alexandair
Guest


 

Re: Problems running an app from within a script

On Jan 28, 10:17*pm, "Aether" <n...@xxxxxx> wrote:
Quote:

> * * *I have a simple and basic question about running external apps from a
> script. *If, within a script, I have this:
>
> -----------------------------------------
>
> $Path = "C:\Temp"
>
> TestApp.exe "$Path\File.txt"
>
> -----------------------------------------
>
> * * *...then it works fine.
>
> * * *However, if I have this:
>
> -----------------------------------------
>
> $TextEditor = "TestApp.exe"
> $Path = "C:\Temp"
>
> $TextEditor + " " + "`"$Path\File.txt`""
>
> -----------------------------------------
>
> * * *...what happens is that the line is displayed like a text string, but
> never actually executed.
>
> * * *What am I doing wrong?
>
> Steve
That last line IS a text string.

You have to use that string as a -command parameter for cmdlet invoke-
expression (Runs a Windows PowerShell expression that is provided in
the form of a string.) like:

invoke-expression -command ($TextEditor + " " + "`"$Path\File.txt`"")
or

invoke-expression ($TextEditor + " " + "`"$Path\File.txt`"") or even
shorter (if you use it in a console)

iex ($TextEditor + " " + "`"$Path\File.txt`"")

Regards,
Aleksandar
My System SpecsSystem Spec
Old 01-28-2008   #3 (permalink)
Shay Levi
Guest


 

Re: Problems running an app from within a script



Thats because its just a string. Consider this:

PS > "notepad.exe"
notepad.exe

notepad is not executed, it is treated as a string. To invoke it:

$n = "notepad.exe"
$file = "$profile"

invoke-expression "$n $file"

This will load your profile into notepad. Likewise, you can use the Call
operator (&):

& $n $file



-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Quote:

> I have a simple and basic question about running external apps
> from a script. If, within a script, I have this:
>
> -----------------------------------------
>
> $Path = "C:\Temp"
>
> TestApp.exe "$Path\File.txt"
>
> -----------------------------------------
>
> ...then it works fine.
>
> However, if I have this:
>
> -----------------------------------------
>
> $TextEditor = "TestApp.exe"
> $Path = "C:\Temp"
> $TextEditor + " " + "`"$Path\File.txt`""
>
> -----------------------------------------
>
> ...what happens is that the line is displayed like a text string,
> but never actually executed.
>
> What am I doing wrong?
>
> Steve
>

My System SpecsSystem Spec
Old 01-28-2008   #4 (permalink)
Steve
Guest


 

Re: Problems running an app from within a script

"Shay Levi" <no@xxxxxx> wrote in message
news:8766a9441c0df8ca301de3cc292e@xxxxxx
Quote:

>
>
> Thats because its just a string. Consider this:
>
> PS > "notepad.exe"
> notepad.exe
>
> notepad is not executed, it is treated as a string. To invoke it:
>
> $n = "notepad.exe"
> $file = "$profile"
>
> invoke-expression "$n $file"
>
> This will load your profile into notepad. Likewise, you can use the Call
> operator (&):
>
> & $n $file
>


Thank you both, that did the trick.



Steve


My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Running PowerShell Script from TaskScheduler malckelly PowerShell 3 11-22-2007 10:11 PM
Logon script not running? Murphy Wong Vista General 2 08-25-2007 06:44 AM
trouble running a script BJ Daniels PowerShell 3 08-20-2007 03:10 PM
Running an HTA or WSH script as administrator Vista security 3 01-10-2007 10:49 AM
Help on running a a script Toxic PowerShell 3 12-05-2006 11:35 AM


Vistax64.com 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 2005-2008

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 47 48 49 50 51