PowerShell Script Template v2
To coincide with my new PowerShell Logging Module, I have updated my PowerShell Script Template to now use the PSLogging module as opposed to my original PowerShell_Logging function library.
The article below introduces you to the new PowerShell Script Template version 2 and shows you how to implement it in your scripts and how to create PowerShell log files.
Why Use a Script Template?
First off let’s explore some of the benefits you get when using a standardised script template for all (or at least most) of your scripts:
- Easy to use
- Self-Explanatory with heaps of comments
- Standardises the look and feel of your scripts
- Ensures easy readability of your scripts and comments
- Provides your scripts will all the standard PowerShell help and meta information
- Integrates with my PowerShell Logging module (PSLogging) to be able to create easy to use PowerShell log files
- Provides a standard methodology for error handling (using Try and Catch)
- Makes troubleshooting and future updates easier and quicker
PowerShell Script Template Types
As part of my upgrade to version 2 of the PowerShell script template, I have created two different types:
- Template with Logging – includes the PSLogging module import and all of the calling of all of the cmdlets to handle the logging and error handling.
- Template without Logging – a similar template but does not include any logging functionality. This PowerShell Script Template does include error handling.
If you are using the PowerShell Script Template with Logging, then you will need my PSLogging module available on the machine you are running the script from.
To download the PSLogging module and for install instructions see PowerShell Logging – Easily create log files Version 2.
You can download the PowerShell Script Template Version 2 with and without logging from the following locations:
- GitHub PSLogging Repository
- GitHub Gist – PowerShell Template with Logging
- GitHub Gist – PowerShell Template without Logging
- From the code below…
How To use the PowerShell Script Template
Here is some important info that you need to know when using either versions of the templates:
#requires -version 4(line 1) – Specifies that at least version 4 of PowerShell is required to run this script. You can change this to any version as you so require.
$ErrorActionPreference = 'SilentlyContinue'(line 33) – configures PowerShell to not stop on any errors that occur and not display them either (this is required as error handling is handled by
- Functions – These templates are designed so that all your code is written within functions and then each function is called in the Execution part of the template (i.e. line 83 in the logging template and line 59 in the non-logging template)
- Functions – To create a function remove the comment blocks
#>on lines 50 and 78 (or 41 and 55 for the non-logging template). Next you will need to name your function by replacing
<FunctionName>with an appropriate name for your function. It is a good idea to use the PowerShell standard of
Verb-Nounfor function names.
- Multiple Functions – If you require more than one function, simply copy-paste the function already specified in the template for as many functions as you need. All you need to do is replace
< code goes here >with your code for each function required.
- Execution – Once you have written your functions, replace
# Script Execution goes here(line 83 for logging template or line 59 for the non-logging template) with each of the functions in the order you want to run them.
Here is some important information that pertains to the logging template only:
- Before you can use the template, you will need to install the PSLogging module. To do this, see PowerShell Logging – Easily create log files Version 2
Import-Module PSLogging(line 36) – Imports that PSLogging module that handles all of the log file creation and management. Note: PSLogging must be installed on the machine you are running the script on prior to being able to run
$sScriptVersion = '1.0'(line 41) – Specifies the version of your script. This will be written to the header of the log file. This makes it easy to identify if someone is running an older version of your script.
- Log File Path (lines 44 – 46) – Specifies the directory and the file name of the log file you want to create.
PowerShell Script Template (with logging)
If you don’t want to use the download links above, then here is my PowerShell Script Template Version 2 (with logging):
PowerShell Script Template (without logging)
Similarly, if you don’t want to use the download links above, then you can access the PowerShell Script Template Version 2 (without logging) here:
PowerCLI Script Template?
If you are writing scripts to automate any VMware functionality, then you why not take a look at my PowerCLI Script Template Version 2 article which contains a PowerCLI script with the PSLogging module and one without. For more information see – PowerCLI Script Template Version 2.
And that is pretty much it. Hope this helps you create some awesome PowerShell scripts and will especially help you with error handling and managing log files for your PowerShell scripts.
If you have any questions or are unsure of anything within the templates or with the PSLogging module, then let me know in the comments below.