Much has been written about how you need to use Begin/Process/End blocks in order to handle a collection of values passed from the pipeline, but those posts all seem to focus on how to do it in a *function*, not a script.
I recently struggled to figure out how to retrofit this functionality into one of my existing PowerShell scripts.
If you’re here I guess you already know WHY you want to do it, but it’s the HOW what’s causing you distress.
Seen this and had your script dump a list of running processes to screen?
begin : The term 'begin' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling
of the name, or if a path was included, verify that the path is correct and try again.
The trick is simple: there can be NO executable code outside the B/P/E blocks. Nothing. No ‘statics’, no hash tables declared, no functions, not a sausage. Once you move the lot into the Begin block, it’s plain sailing.
Continue reading ‘Begin-Process-End in a PowerShell script’ »