PowerCLI Script Template v2

To coincide with my new PowerShell Logging Module, I have also updated my PowerCLI Script Template to now use the PSLogging module as opposed to my original PowerShell_Logging function library.

This template is based on my PowerShell Script Template Version 2, but has been modified for use with PowerCLI so that you can easily create scripts and solutions to automate your VMware world!

Below is everything you need to know on the PowerCLI template, as well as the template itself…

Introduction

Using PowerShell is awesome and if you manage VMware, then you would know that PowerCLI is a very powerful tool. The thing is that when we need to achieve something using PowerShell or PowerCLI, we generally write a quick, messy script and then store it somewhere on our computer.

After 6 months or so, when we need to do something similar, we go back to that script and spend a long time trying to work out how it works and why we did what we did. Generally at that point, you wish you had documented it a bit better and had added some commenting.

To solve this problem…. we use a template! So I created a template just for PowerCLI, which takes care of adding in the PowerCLI snap-in, connecting to a host \ vCenter, etc. All you need to do is write the meaty bits of the script and it will always be beautifully presented and documented, so that when you go back in 6 months time you will find it much easier to quickly understand what is going on.

Why Use a Script Template?

Here are some of the benefits you get when using a standardised script template for all (or at least most) of your scripts:

  • Consistency
  • 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 PowerCLI script template, I have created two different types:

  1. 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.
  2. Template without Logging – a similar template but does not include any logging functionality. This PowerShell Script Template does include error handling.

Template Pre-Requisites

If you are using the PowerCLI 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.

Download Links

You can download the PowerCLI Script Template Version 2 with and without logging from the following locations:

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 Try...Catch.
  • 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 <# and #> 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-Noun for 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.
  • Connect-VMwareServer is the function used to connect to your vCenter server or an ESXi host. You do not need to hard-code the vCenter server or ESXi host you want to connect to in the script, as line 116 in the logging template or line 78 in the non-logging template will prompt you for the details at run-time. If you don’t not want this (as you are running your script on a schedule), you can remove this line and hard-code the server details as required.

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 Import-Module PSLogging.
  • $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.

PowerCLI Script Template (with logging)

If you don’t want to use the download links above, then here is my PowerCLI Script Template Version 2 (with logging):

PowerCLI Script Template (without logging)

Similarly, if you don’t want to use the download links above, then you can access the PowerCLI Script Template Version 2 (without logging) here:

PowerShell Script Template?

If you are not wiring scripts for VMware, then why not check out my standard PowerShell Script Template which has been designed for any generic PowerShell script, whether that be for VMware or any other solution.

The PowerShell Script Template has also been designed with two versions, one with logging (via PSLogging) and one without. To get more information and for the download links, see – PowerShell Script Template Version 2.

If you have any questions or need clarification or help with any of the components of the template then shoot me an email or let me know in the comments below.

Happy coding!
Luca

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.