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 - Execute a command in a string

Reply
 
Old 01-02-2007   #1 (permalink)
Alan van der Vyver


 
 

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.

My System SpecsSystem Spec
Old 01-02-2007   #2 (permalink)
Rob Campbell


 
 

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.
>

My System SpecsSystem Spec
Old 01-02-2007   #3 (permalink)
Keith Hill


 
 

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

My System SpecsSystem Spec
Old 01-02-2007   #4 (permalink)
Jeffrey Snover [MSFT]


 
 

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.


My System SpecsSystem Spec
Old 01-03-2007   #5 (permalink)
Keith Hill


 
 

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

My System SpecsSystem Spec
Old 01-03-2007   #6 (permalink)
Alex K. Angelopoulos [MVP]


 
 

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:\


My System SpecsSystem Spec
Old 01-03-2007   #7 (permalink)
Keith Hill


 
 

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

My System SpecsSystem Spec
Old 01-04-2007   #8 (permalink)
Keith Hill


 
 

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
My System SpecsSystem Spec
Old 01-09-2007   #9 (permalink)
Lee Holmes [MSFT]


 
 

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


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Windows Mail Command "The Command Failed To Execute" Vista mail
Command Failed To Execute Vista mail
The command failed to execute Vista mail
The Command failed to execute Vista mail
command : execute Vista General


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