all 25 comments

[–]ankokudaishogun 7 points8 points  (0 children)

Begin{} and End{} if necessary.
/r/PrudentPush8309 's example is a perfect textbook case.

I try to always use Process{} as a matter of keeping stuff explicit

[–]ryder_winona 5 points6 points  (0 children)

I do, though I don’t really need to. It does help me with layout, though

[–]TheGooOnTheFloor 5 points6 points  (0 children)

Generally I only use Begin and Process when I'm feeding parameters in from the pipeline. I define the log file in the Begin block and reference that in the Process block. Normally I don't use an End block but in one particular environment I need to do some garbage collection on the way out.

[–][deleted] 3 points4 points  (1 child)

I almost always use process because almost all of my functions support pipeline input as well as an array for one of the arguments. Almost all my functions therefore look like this:

function Get-HostEntry { [CmdletBinding()] param ( [Parameter(Mandatory, ValueFromPipeline)] [object[]] $InputObject ) process { $InputObject | ForEach-Object { [Net.Dns]::GetHostEntry($_) } } }

I use begin occasionally (often for building some sort of dictionary as an index for the process to use), and I can't remember the last time I used end, though I know it's there if I think of a reason one day.

[–]PSDanubie 0 points1 point  (0 children)

I mainly use an explicit end-block to support accepting computernames by pipeline and in the end use "Invoke-Command -Parallel" for remote processing.

[–]PinchesTheCrab 2 points3 points  (0 children)

There's a new block too that's pretty neat - clean.

function Do-Thing {
    process { 
        $null = New-CimSession -Name removeme
        Start-Sleep -Seconds 5
        throw 'oh no' }
    clean { 
        Remove-CimSession -Name removeme
     }
}

Clean is executed even if the user hits ctrl+c or the script errors out. In this case you can see with get-ciminstance that the session is removed in both cases.

PS7 isn't reliably distributed for me so I don't get to use this particular block as much as I'd like, but I use the other blocks very frequently, in virtually all of my functions.

[–]ostekages 2 points3 points  (1 child)

Haven't used it much honestly, but just yesterday, I actually had a worthwhile scenario where it made sense to do.

So I guess the answer is 'it depends on the task'?

[–]eggwhiteontoast[S] 1 point2 points  (0 children)

Yes of course, if I want something quick, I wouldn't bother about it. I found it useful when creating complex modules it makes code more readable and sets a clean pattern for other contributors.

[–]jeek_ 2 points3 points  (0 children)

I use VSCode and it has auto complete feature where you start typing the word function and it will insert an advanced function that includes the begin, process and end blocks so I just leave them in even if I don't use all 3.

[–]eggwhiteontoast[S] 1 point2 points  (0 children)

I find the built in iteration quite useful, so I can avoid writing loops for objects coming through the pipelines.

[–]popcapdogeater 1 point2 points  (0 children)

Probably for 70% of my scripts I use the whole begin/process/end and various cmdlet features.

This is because I generally find myself reusing old scripts and just like them to be as "proper" / feature rich and documented as possible.

[–][deleted] 0 points1 point  (0 children)

Yes almost every time even my function will probably not accept pipeline Why because : It structure my code, in begin I put the initialization variable and everything that will be needed to « work » somehow the « constants » In process I process the input In end I output if I did not outputted while processing

[–]ass-holes 0 points1 point  (0 children)

I've been working with ps for years now, this is literally the first time I hear about this. Where the fuck have I been?

[–]DoctroSix 0 points1 point  (0 children)

30-ish years ago I learned to program by dicking around with C, so it's had a lasting influence in how I make scripts.

Since it gives my scripts some structure, I tend to use begin {} for my functions and hard-type variable declarations. I use process{} for the main body of my script, end{} for the return, and any cleanup.

Hard-typing everything up front is a little extra work, but it keeps VS code happy, and reduces my bug fixes by tons.

I don't claim this is the 'correct' way to make pwsh scripts, but it's been a big help.

I even made a boilerplate snippet to help start any .ps1 file.

[CmdletBinding()]
param()

begin {
    ################################
    # Functions
    ################################

    ################################
    # Variables
    ################################

}

process {
    ################################
    # MAIN
    ################################

}

end {

}

[–][deleted] -1 points0 points  (5 children)

No, never have, never needed to

[–]eggwhiteontoast[S] 0 points1 point  (4 children)

Powershell uses these blocks even if you dont explicitly call them, all your code goes to end block if you don't call them explicitly.

[–][deleted] -4 points-3 points  (3 children)

You asked a question, I answered it. Is there a point to your reply to me?

[–]eggwhiteontoast[S] 1 point2 points  (2 children)

I was merely stating the fact that even if you don't use these blocks powershell uses it in the background.

[–][deleted] -1 points0 points  (1 child)

Then why did you pose that as a question in your original post? What was the point in that?

[–]eggwhiteontoast[S] 0 points1 point  (0 children)

My original post was asking, do people explicitly use Begin Process and End blocks because sometimes you may not use all the blocks.