Scripting with the intention to reuse
Changing gears from storing the scripts is the ability to repurpose them, or use portions of a script in multiple areas. Functions, workflows, and modules are critical to this end. Each will be described in detail in this section of the book.
Functions, functions, functions – the fun with functions
What is a function? The help
file defines it as a named block of code that performs an action. So writing a function would look like this:
Function Get-PS {get-process PowerShell}
Here, the output would be nothing until the function is called like this:
Function Get-PS {get-process PowerShell} Get-PS
The output of this is the same as Get-Process PowerShell
but it is reusable and could be called at any point in the script. There is a simple and useful function that I personally use in many of the developed scripts to get a decision and return true
or false
:
Function Get-Decision ($Decision) { Switch ($Decision.toupper()) { "Y" {$Decision =...