Windows Vista Forums

BEGIN-PROCESS-END sample does not work
  1. #1


    ktmd Guest

    BEGIN-PROCESS-END sample does not work

    I ran the following sample from Powershell Quick Reference, but I got error.
    Can you tell why?

    POSH >Get-Process |foreach {BEGIN{$x=1}
    >> PROCESS{$X++}
    >> END{"$X Processes"}}
    >>

    ForEach-Object : The script block cannot be invoked because it contains more
    than one clause. The Invoke() method can o
    nly be used on script blocks containing a single clause.
    At line:1 char:21
    + Get-Process |foreach <<<< {BEGIN{$x=1}





      My System SpecsSystem Spec

  2. #2


    Andrew Savinykh Guest

    Re: BEGIN-PROCESS-END sample does not work

    Yeah, the documentation certainly can be improved. I think they ment

    Get-Process | &{BEGIN{$x=1} PROCESS{$X++} END{"$X Processes"}}

    ktmd wrote:
    > I ran the following sample from Powershell Quick Reference, but I got error.
    > Can you tell why?
    >
    > POSH >Get-Process |foreach {BEGIN{$x=1}
    >>> PROCESS{$X++}
    >>> END{"$X Processes"}}
    >>>

    > ForEach-Object : The script block cannot be invoked because it contains more
    > than one clause. The Invoke() method can o
    > nly be used on script blocks containing a single clause.
    > At line:1 char:21
    > + Get-Process |foreach <<<< {BEGIN{$x=1}
    >
    >


      My System SpecsSystem Spec

  3. #3


    Keith Hill Guest

    Re: BEGIN-PROCESS-END sample does not work


    "ktmd" <ktmd@discussions.microsoft.com> wrote in message
    news:896BFDC3-6188-4DC3-AFED-89EA7693E2D5@microsoft.com...
    >I ran the following sample from Powershell Quick Reference, but I got
    >error.
    > Can you tell why?
    >
    > POSH >Get-Process |foreach {BEGIN{$x=1}
    >>> PROCESS{$X++}
    >>> END{"$X Processes"}}


    Begin, Process, End are parameters on the Foreach-Object cmdlet. So you can
    be explicit like so:

    Get-Process | foreach -BEGIN{$x=1} -PROCESS{$X++} -END{"$X Processes"}

    or you can rely on positional parameters:

    Get-Process | foreach {$x=1} {$X++} {"$X Processes"}

    --
    Keith


      My System SpecsSystem Spec

  4. #4


    ktmd Guest

    Re: BEGIN-PROCESS-END sample does not work

    All of your solutions work. The documentation is also wrong to start array
    count with $x=1, it should be $x=0.

    Thanks for your help.

    ktmd

    "Keith Hill" wrote:

    >
    > "ktmd" <ktmd@discussions.microsoft.com> wrote in message
    > news:896BFDC3-6188-4DC3-AFED-89EA7693E2D5@microsoft.com...
    > >I ran the following sample from Powershell Quick Reference, but I got
    > >error.
    > > Can you tell why?
    > >
    > > POSH >Get-Process |foreach {BEGIN{$x=1}
    > >>> PROCESS{$X++}
    > >>> END{"$X Processes"}}

    >
    > Begin, Process, End are parameters on the Foreach-Object cmdlet. So you can
    > be explicit like so:
    >
    > Get-Process | foreach -BEGIN{$x=1} -PROCESS{$X++} -END{"$X Processes"}
    >
    > or you can rely on positional parameters:
    >
    > Get-Process | foreach {$x=1} {$X++} {"$X Processes"}
    >
    > --
    > Keith
    >
    >


      My System SpecsSystem Spec

  5. #5


    Krishna Vutukuri[MSFT] Guest

    Re: BEGIN-PROCESS-END sample does not work

    Hi,

    Can you open a bug on the documentation error at
    http://connect.microsoft.com.

    --
    Thanks
    Krishna[MSFT]
    Windows PowerShell Team
    Microsoft Corporation
    This posting is provided "AS IS" with no warranties, and confers no rights.

    "ktmd" <ktmd@discussions.microsoft.com> wrote in message
    news:16AAFBA9-8F4C-4C57-A396-1FA9781D26FE@microsoft.com...
    > All of your solutions work. The documentation is also wrong to start
    > array
    > count with $x=1, it should be $x=0.
    >
    > Thanks for your help.
    >
    > ktmd
    >
    > "Keith Hill" wrote:
    >
    >>
    >> "ktmd" <ktmd@discussions.microsoft.com> wrote in message
    >> news:896BFDC3-6188-4DC3-AFED-89EA7693E2D5@microsoft.com...
    >> >I ran the following sample from Powershell Quick Reference, but I got
    >> >error.
    >> > Can you tell why?
    >> >
    >> > POSH >Get-Process |foreach {BEGIN{$x=1}
    >> >>> PROCESS{$X++}
    >> >>> END{"$X Processes"}}

    >>
    >> Begin, Process, End are parameters on the Foreach-Object cmdlet. So you
    >> can
    >> be explicit like so:
    >>
    >> Get-Process | foreach -BEGIN{$x=1} -PROCESS{$X++} -END{"$X Processes"}
    >>
    >> or you can rely on positional parameters:
    >>
    >> Get-Process | foreach {$x=1} {$X++} {"$X Processes"}
    >>
    >> --
    >> Keith
    >>
    >>




      My System SpecsSystem Spec

  6. #6


    ktmd Guest

    Re: BEGIN-PROCESS-END sample does not work

    Krishna,
    I do not see connection info for Powershell on connect.microsoft.com.
    ktmd

    "Krishna Vutukuri[MSFT]" wrote:

    > Hi,
    >
    > Can you open a bug on the documentation error at
    > http://connect.microsoft.com.
    >
    > --
    > Thanks
    > Krishna[MSFT]
    > Windows PowerShell Team
    > Microsoft Corporation
    > This posting is provided "AS IS" with no warranties, and confers no rights.
    >
    > "ktmd" <ktmd@discussions.microsoft.com> wrote in message
    > news:16AAFBA9-8F4C-4C57-A396-1FA9781D26FE@microsoft.com...
    > > All of your solutions work. The documentation is also wrong to start
    > > array
    > > count with $x=1, it should be $x=0.
    > >
    > > Thanks for your help.
    > >
    > > ktmd
    > >
    > > "Keith Hill" wrote:
    > >
    > >>
    > >> "ktmd" <ktmd@discussions.microsoft.com> wrote in message
    > >> news:896BFDC3-6188-4DC3-AFED-89EA7693E2D5@microsoft.com...
    > >> >I ran the following sample from Powershell Quick Reference, but I got
    > >> >error.
    > >> > Can you tell why?
    > >> >
    > >> > POSH >Get-Process |foreach {BEGIN{$x=1}
    > >> >>> PROCESS{$X++}
    > >> >>> END{"$X Processes"}}
    > >>
    > >> Begin, Process, End are parameters on the Foreach-Object cmdlet. So you
    > >> can
    > >> be explicit like so:
    > >>
    > >> Get-Process | foreach -BEGIN{$x=1} -PROCESS{$X++} -END{"$X Processes"}
    > >>
    > >> or you can rely on positional parameters:
    > >>
    > >> Get-Process | foreach {$x=1} {$X++} {"$X Processes"}
    > >>
    > >> --
    > >> Keith
    > >>
    > >>

    >
    >
    >


      My System SpecsSystem Spec

BEGIN-PROCESS-END sample does not work problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Failover Guest Cluster -- 'The process cannot access the file becauseit is being used by another process.' J Wolfgang Goerlich Virtual Server 0 06 Feb 2009
TFS 2008 Process Template: Default value tag for work item fieldsdoes not allow more than 255 characters to be entered into an HTML control Steve .NET General 0 23 Jul 2008
WMP11 SRS WOW does not work with 96Khz and 192Khz sample output ra pgruebele Vista music pictures video 5 18 Jul 2008
Multi-boot setup process does not work as advertised in FAQs Aymincendiary Vista installation & setup 5 27 Aug 2007
foreach, foreach-object & begin/process/end scriptblock clauses... Clint Bergman PowerShell 12 16 May 2007