Skip to main content

Posts

Showing posts from March, 2019

Where to Create a Constant in an Advanced Function?

This week I am delivering a PowerShell Toolmaking class in Phoenix.   Right now we are studying advanced functions.   While talking about the BEGIN, PROCESS, and END block, one of my future PowerShell Rock Stars gave me a great idea.   Where should a constant be created in an advanced function> A constant is, by definition, something that cannot be changed after it is created.   So, here is how you create one. $ CONST = New-Variable -Name Constant1 -Value 10 -Option Constant You cannot modify this constant with Set-Variable -Force and you cannot remove this constant with Remove-Variable -Force.   That is the idea.   You need to close your current scope of memory to get rid of it. The BEGIN, PROCESS, and END blocks of an advanced function have unique capabilities. The BEGIN block is executed when your cmdlet receives the first object in the pipeline, but before the PROCESS block is executed.   It is only ran once to allow you the opportunity to do any set up