Zohnya
Zohnya

Reputation: 25

Using the Begin-process-end for Powershell. What are the benefits?

i'm currently trying to do a small script to retrieve data from a server using REST endpoint. As it's the first time i work with powershell, i'm a bit disappointed by the 'lack' of structure and 'typing' in it. Then i structured my code like this with comments :

# declare ------------------------------------------------------------------------------------------
   // All the var i'll need in my process
# do -----------------------------------------------------------------------------------------------
  // Ask for user to enter URL and credentials
  // Check if credentials are correct
  // Connect to the server
  // Retrieves the data in a list (JSON formatted) --> List  REST EndPoint

  // Foreach document in my list 
     // retrieve the document's details --> single file REST EndPoint
     // download the file into local directory
  // End Foreach 

# display results ----------------------------------------------------------------------------------
// display :
   // Downloaded files
   // Non-Downloaded files

During review, my colleague told me "Oh ! What you need is the 'begin-process-end' " and then just leave.

I then read somethings about this here but for what i see, the structure is the same as i did with my comments but i don't see the point where it's "What i need" as a functionalities.

Since i'm a real beginner, i maybe miss the point. Could you explain it to me ?

(btw, thx to the kind person who'll edit my ugly english mistakes)

Upvotes: 0

Views: 436

Answers (3)

Ian Cammarata
Ian Cammarata

Reputation: 11

Was going to post this as a comment to js2010's answer, but not enough reputation...

Here's the link to the official documentation: Microsoft Learn / PowerShell / about_Functions_Advanced_Methods Of particular interest is the CLEAN block new to PowerShell 7.3; functioning similar to a finally block in a try/catch.

Upvotes: 0

js2010
js2010

Reputation: 27491

Being/process/end is really for pipelines. You can't process from the pipe without a process block.

1..5 | & { process {$_ * 2} }

2
4
6
8
10

Upvotes: 1

Vish
Vish

Reputation: 466

Think of these blocks as a pre-processor (the begin block), actual processor (the process block), and a post-processor(the end block) to a function.

You don't need to define any of these blocks (although Begin and End block will always need a Process block), and can write code just fine without them, but the idea behind them is to divide the function into three separate areas of code. The following function should make it a little clearer:

function addition_by_one {
# Arguments to a powershell functions are defined inside the param block.
  Param (
      [int]$number
  )
# In the below begin block, the variable $a was initialized to 1. 
  Begin {
    $a = 1
  }
# The Process block does the main work. In this case, $a is added to the argument supplied to the function.
  Process {
    $sum = $number + $a
  }
# Finally, the End block will execute after the process block completes.
  End {
    "Sum is $sum"
  }
}
# Call the function with any integer argument.
addition_by_one -number 3

Upvotes: 1

Related Questions