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

foreach-object question

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 11-08-2006   #1 (permalink)
~Clint
Guest


 

foreach-object question

why does this not work:
foreach-object {
begin { foo }
process { foo }
end { foo }
}

I get this error:
ForEach-Object : The script block cannot be invoked because it contains more
than one clause. The Invoke() method can only be used on script blocks
containing
a single clause.
At line:1 char:49
+ $users | select-object -first 5 | foreach-object <<<< {

Any guidance is most appreciated. Thanks in advance.
--
v(^_^)v

My System SpecsSystem Spec
Old 11-08-2006   #2 (permalink)
~Clint
Guest


 

RE: foreach-object question

get-help about_foreach

Never Mind.
My System SpecsSystem Spec
Old 11-08-2006   #3 (permalink)
James Truher
Guest


 

Re: foreach-object question

foreach-object has it's script blocks assigned via parameters and creates
it's own complete scriptblock, so you need to do this:

foreach-object -begin { "foo" } -process { "foo" } -end { "foo" }

internally, foreach creates a complete script block out of the pieces



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

"~Clint" <Clint@discussions.microsoft.com> wrote in message
news:C11382C1-07AD-4AC8-9513-8CF2828879F3@microsoft.com...
> why does this not work:
> foreach-object {
> begin { foo }
> process { foo }
> end { foo }
> }
>
> I get this error:
> ForEach-Object : The script block cannot be invoked because it contains
> more
> than one clause. The Invoke() method can only be used on script blocks
> containing
> a single clause.
> At line:1 char:49
> + $users | select-object -first 5 | foreach-object <<<< {
>
> Any guidance is most appreciated. Thanks in advance.
> --
> v(^_^)v



My System SpecsSystem Spec
Old 11-08-2006   #4 (permalink)
Keith Hill [MVP]
Guest


 

Re: foreach-object question

"James Truher" <jimtru@news.microsoft.com> wrote in message
news:%234gx5u2AHHA.3316@TK2MSFTNGP02.phx.gbl...
> foreach-object has it's script blocks assigned via parameters and creates
> it's own complete scriptblock, so you need to do this:
>
> foreach-object -begin { "foo" } -process { "foo" } -end { "foo" }


Alternatively you can do this:

1..5 | & {begin{"foo"}process{"foo"}end{"foo"}}

or

1..5 | & {process{"p"}}

Sometimes I prefer this to foreach-object because it maps better to my
mental model of using an anonymous PoSH function/scriptblock in the pipeline
versus "iteration" that is implied by the name "foreach". I always have a
hard time explaining foreach-each since I have usually just explained that
objects are passed down the pipeline one at a time to the next stage by the
object flow engine. So the question that usually comes up is "then why do I
need to iterate over individual pipeline objects?". If you have a better
way to explain this I would appreciate hearing it. One benefit of the
cmdlet foreach-object is that you get the functionality given by the
ubiqitous parameters (-ErrorAction, etc).

--
Keith


My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Oddity with ForEach-Object or is it me? Kiron PowerShell 0 05-09-2008 11:16 AM
foreach, foreach-object & begin/process/end scriptblock clauses... Clint Bergman PowerShell 12 05-16-2007 05:30 PM
Difference in semantics of for, foreach and foreach-object Andrew Watt [MVP] PowerShell 3 01-26-2007 12:46 PM
ForEach-Object and -InputObject - Any use for it? Alex K. Angelopoulos [MVP] PowerShell 8 10-27-2006 12:41 AM
Peculiarity/bug of foreach-object klumsy@xtra.co.nz PowerShell 8 10-12-2006 12:00 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 51