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

Execute a command in a string

Closed Thread
 
Thread Tools Display Modes
Old 01-02-2007   #1 (permalink)
Alan van der Vyver
Guest


 

Execute a command in a string

Hi!

I am having remarkable difficulty with something I expected to be really
easy. In a script, I want to create a string containing a complex
command with its parameters derived from the script's command line
arguments and then execute it.

If I do something like:

$c = "get-childitem"
&$c

it works as expected, but if I do something like:

$c = "get-childitem d:\temp"
&$c

it does not recognize the command.

Can anyone tell me how to do this?

regards,
Alan.
Old 01-02-2007   #2 (permalink)
Rob Campbell
Guest


 

RE: Execute a command in a string

Use invoke-expression in place of &.

You'll need someone with more savvy on the internals than I to explain the
nuances of the difference between "invoke-expression" and & (invoke command).

"Alan van der Vyver" wrote:

> Hi!
>
> I am having remarkable difficulty with something I expected to be really
> easy. In a script, I want to create a string containing a complex
> command with its parameters derived from the script's command line
> arguments and then execute it.
>
> If I do something like:
>
> $c = "get-childitem"
> &$c
>
> it works as expected, but if I do something like:
>
> $c = "get-childitem d:\temp"
> &$c
>
> it does not recognize the command.
>
> Can anyone tell me how to do this?
>
> regards,
> Alan.
>

Old 01-02-2007   #3 (permalink)
Keith Hill
Guest


 

Re: Execute a command in a string

"Rob Campbell" <RobCampbell@discussions.microsoft.com> wrote in message
news:40A34E15-3024-4CDC-8F26-C1A105E2BD6B@microsoft.com...
> Use invoke-expression in place of &.
>
> You'll need someone with more savvy on the internals than I to explain the
> nuances of the difference between "invoke-expression" and & (invoke
> command).


For what you are trying to do Invoke-Expression is the way to go as Rob
points out since it operates on strings that contain script code. The & or
call operator is used to invoke "commands" and script blocks. I *think* the
call operator was introduced to kick the PoSH parser into command mode.
Here's the problem. When you type a string at the prompt and press enter,
PoSH evaluates in expression mode based on the first non-whitespace
character being a quote (double or single) e.g.:

> "Hello"

Hello

Normally that is what you want except when the string is a path containing
spaces to some native exe that you want to execute. You have to quote the
path or the parse will view the path as two different chunks e.g.:

"C:\program files\foo\foo.exe"

except that that only spits the string out to the console (it's in
expression parsing mode). So the way you get PoSH to "call" the command
specified in quotes is to use the call operator:

& "C:\program files\foo\foo.exe"

I suspect that PoSH tries to execute the command specified exactly by the
string. In your case, there is no command named "Get-ChildItem d:\temp".
Anyway you can also store code in a script block and then later execute it
using the call operator:

$code = {get-date}
&$code

HTH,
Keith

Old 01-02-2007   #4 (permalink)
Jeffrey Snover [MSFT]
Guest


 

Re: Execute a command in a string

The way it works is that & executes a TOKEN (or a SCRIPTBLOCK) not an
EXPRESSION or COMMAND.

--
Jeffrey Snover [MSFT]
Windows PowerShell Architect
Microsoft Corporation
This posting is provided "AS IS" with no warranties, no confers rights.


Old 01-02-2007   #5 (permalink)
Keith Hill
Guest


 

Re: Execute a command in a string

"Jeffrey Snover [MSFT]" <jsnover@ntdev.microsoft.com> wrote in message
news:571D5D46-7BFE-4864-919B-019D22EF8C35@microsoft.com...
> The way it works is that & executes a TOKEN (or a SCRIPTBLOCK) not an
> EXPRESSION or COMMAND.
>


TOKEN is a rather opaque term. Is it a terminal in the language? If not,
can you give us an idea on what the E/BNF production for TOKEN is?

--
Keith

Old 01-03-2007   #6 (permalink)
Alex K. Angelopoulos [MVP]
Guest


 

Re: Execute a command in a string

"Keith Hill" <r_keith_hill@mailhot.nospamIdotcom> wrote in message
news:02403A2F-9A32-473C-8140-28547B7BCDA0@microsoft.com...
> "Jeffrey Snover [MSFT]" <jsnover@ntdev.microsoft.com> wrote in message
> news:571D5D46-7BFE-4864-919B-019D22EF8C35@microsoft.com...
>> The way it works is that & executes a TOKEN (or a SCRIPTBLOCK) not an
>> EXPRESSION or COMMAND.
>>

>
> TOKEN is a rather opaque term. Is it a terminal in the language? If not,
> can you give us an idea on what the E/BNF production for TOKEN is?


I'm not sure I get this either. After, all, this works (and would not work
with Invoke-Expression in the raw form):

& gci c:\


Old 01-03-2007   #7 (permalink)
Keith Hill
Guest


 

Re: Execute a command in a string

"Alex K. Angelopoulos [MVP]" <aka@online.mvps.org> wrote in message
news:O32rRA1LHHA.1280@TK2MSFTNGP04.phx.gbl...
> "Keith Hill" <r_keith_hill@mailhot.nospamIdotcom> wrote in message
> news:02403A2F-9A32-473C-8140-28547B7BCDA0@microsoft.com...
>> "Jeffrey Snover [MSFT]" <jsnover@ntdev.microsoft.com> wrote in message
>> news:571D5D46-7BFE-4864-919B-019D22EF8C35@microsoft.com...
>>> The way it works is that & executes a TOKEN (or a SCRIPTBLOCK) not an
>>> EXPRESSION or COMMAND.
>>>

>>
>> TOKEN is a rather opaque term. Is it a terminal in the language? If
>> not, can you give us an idea on what the E/BNF production for TOKEN is?

>
> I'm not sure I get this either. After, all, this works (and would not work
> with Invoke-Expression in the raw form):
>
> & gci c:\


That example makes sense to me since that the call operator works on the
next argument or 'gci' in this example anything after that apparently is
passed to the invoked command as arguments. What I don't get is that we
know "TOKEN" can be a string and can be a variable ($code). Apparently it
can't be an expression which means that this shouldn't work "& (get-command
get-date)" but I can't test that right now because the PC I'm on doesn't
have PowerShell installed (grrr).

BTW Bruce's book "Windows PowerShell In Action" discusses the call operator
in chapter 8. It is an *excellent* book especially if you really want to
understand the ins and outs of the PowerShell language. If you can't wait
for the hard copy, they have early access versions available for purchase:

http://www.manning.com/payette/

--
Keith

Old 01-03-2007   #8 (permalink)
Keith Hill
Guest


 

Re: Execute a command in a string

"Jeffrey Snover [MSFT]" <jsnover@ntdev.microsoft.com> wrote in message news:571D5D46-7BFE-4864-919B-019D22EF8C35@microsoft.com...
> The way it works is that & executes a TOKEN (or a SCRIPTBLOCK) not an
> EXPRESSION or COMMAND.
>


Wouldn't this be an example of executing an expression?

PS C:\Documents and Settings\Keith> & ("g" + "c" + "i")


Directory: Microsoft.PowerShell.Core\FileSystem::C:\Documents and Settings\Keith


Mode LastWriteTime Length Name
---- ------------- ------ ----
d---s 8/20/2006 10:31 PM Cookies
d---- 12/26/2006 12:09 PM Desktop
d-r-- 10/15/2004 10:22 PM Favorites

I understand that it doesn't just execute an expression per se but it does execute the result of an expression *iff* that result corresponds to a discoverable command, right?

--
Keith
Old 01-03-2007   #9 (permalink)
Keith Hill
Guest


 

Re: Execute a command in a string

"Jeffrey Snover [MSFT]" <jsnover@ntdev.microsoft.com> wrote in message news:571D5D46-7BFE-4864-919B-019D22EF8C35@microsoft.com...
> The way it works is that & executes a TOKEN (or a SCRIPTBLOCK) not an
> EXPRESSION or COMMAND.
>


Wouldn't this be an example of executing an expression?

PS C:\Documents and Settings\Keith> & ("g" + "c" + "i")


Directory: Microsoft.PowerShell.Core\FileSystem::C:\Documents and Settings\Keith


Mode LastWriteTime Length Name
---- ------------- ------ ----
d---s 8/20/2006 10:31 PM Cookies
d---- 12/26/2006 12:09 PM Desktop
d-r-- 10/15/2004 10:22 PM Favorites

I understand that it doesn't just execute an expression per se but it does execute the result of an expression *iff* that result corresponds to a discoverable command, right?

--
Keith
Old 01-09-2007   #10 (permalink)
Lee Holmes [MSFT]
Guest


 

Re: Execute a command in a string

("g" + "c" + "i") is not an expression, though. When you use parentheses,
PowerShell starts a sub-parse. To the parser, you have just typed

& gci

--
Lee Holmes [MSFT]
Windows PowerShell Development
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.


"Keith Hill" <r_keith_hill@mailhot.nospamIdotcom> wrote in message
news:7730A3C2-68DC-4828-ACDB-7840FFB3D83A@microsoft.com...
"Jeffrey Snover [MSFT]" <jsnover@ntdev.microsoft.com> wrote in message
news:571D5D46-7BFE-4864-919B-019D22EF8C35@microsoft.com...
> The way it works is that & executes a TOKEN (or a SCRIPTBLOCK) not an
> EXPRESSION or COMMAND.
>


Wouldn't this be an example of executing an expression?

PS C:\Documents and Settings\Keith> & ("g" + "c" + "i")


Directory: Microsoft.PowerShell.Core\FileSystem::C:\Documents and
Settings\Keith


Mode LastWriteTime Length Name
---- ------------- ------ ----
d---s 8/20/2006 10:31 PM Cookies
d---- 12/26/2006 12:09 PM Desktop
d-r-- 10/15/2004 10:22 PM Favorites

I understand that it doesn't just execute an expression per se but it does
execute the result of an expression *iff* that result corresponds to a
discoverable command, right?

--
Keith


Closed Thread

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
command : execute FGNIco Vista General 3 05-10-2008 12:09 PM
redundant questions when you execute any command Stop stupid questions after executions Vista General 4 01-14-2008 08:03 AM
execute command on remote system Justin Rich PowerShell 3 11-30-2007 08:40 AM
RE: Execute from command line with environment variable in path dreeschkind PowerShell 0 11-29-2006 05:08 PM
Execute from command line with environment variable in path Ken Brubaker PowerShell 0 11-29-2006 04:33 PM








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