Begin, Process, End – not just for functions

You frequently see functions written using the Begin, Process, and End keywords with their script blocks.

Something that seems to be a well-kept secret is that they don’t have to be in a function, or even in a script block.

Some people don’t like the restriction imposed by the interpreted nature of powershell scripts that dictates the functions have to be declared before they can be used. They want to see their main code at the top of the script, and the functions defined further down. Conventional wisdom is that you do that using this kind of construction:

function main {
     test1
     test2
     }

function test1 {
     function script
     }
function test2 {
     function script
     }
main

You can accomplish the same thing using the Begin and End keywords:


end {
     test1
     test2
     }

begin {
function test1 {
     function script
     }
function test2 {
     function script
     }
}

The Begin and End keyworks can be used outside of a function or script block, and can be declared in whatever order you want, so you can declare all you functions in a Begin block, but do it at the end of the script.

It also means you can create a “filter script” by using the Process keyword in the script:


'Process {Write-host $_ -fore "Yellow" -back "Black"}' | sc filter.ps1

gci | ./filter.ps1

Advertisements

8 responses to “Begin, Process, End – not just for functions

  1. How to abort function in begin block and skipprocess and end blocks.

    • function test{

      End {
      if ($abort){return}
      ‘this is the end’
      }

      Process {

      if ($abort){return}
      ‘this is process’
      }

      Begin {
      ‘this is the beginning’
      $abort = $true
      }
      }

  2. Thanks – I thought of that but I was hoping there was a more immediate way.
    Throwing a terminating error might work.

    • Return doesn’t seem to work in the Begin block, but Exit does, and Break does.

      function test{

      End {
      ‘this is the end’
      }

      Process {

      ‘this is process’
      }

      Begin {
      ‘this is the beginning’
      break
      }
      }

      • Yes – it exits completely from PowerShell.which is not really all that helpful.
        I guess the flag method will do for now.

      • Try Break.

        That seems to quietly terminate the function an return control without terminating the session.

      • That is interestig. I tried that before and it didn’t work.. Now it does. I must have mucked something up.

        Thanks. That can be very useful. I want to clean up in end{} but not if ther is an exception and I don’t wnat to rely on a flag as it can be an issue. ‘break’ works even outside of try/catch which we would not expect.

  3. Pingback: Event 3: My way… | IT Pro PowerShell experience

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s