![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
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.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| 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 Specs![]() |
| | #2 (permalink) |
| Guest | RE: foreach-object question get-help about_foreach Never Mind. |
My System Specs![]() |
| | #3 (permalink) |
| 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 Specs![]() |
| | #4 (permalink) |
| 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 Specs![]() |
![]() |
| 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 |