PowerCLI Script Template

Important Update

As of September 2015, I have released version 2 of my PowerCLI Script Template, which includes a number of improvements over this version, in particular the use of the new PSLogging PowerShell module that significantly improves the creation and management of log files. This version of the template is no longer supported or maintained, so please upgrade to the new version today!

Version 2 of the template is available here – PowerCLI Script Template v2.


A while a go I posted up my PowerShell Script Template which turned out to be pretty popular with fellow PowerShellers.

I recently create another slightly modified template when working with PowerCLI, so I thought I would share it here. Here is my PowerCLI Script Template to be used for all your VMware scripts…

Intro

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 template?

We pretty much covered why to use a template above, but I thought I would list the major benefits below:

  • Easy to use
  • Self-Exlpanatory 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 recommended meta information
  • Integrates with my PowerShell Logging Function Library for easy to use script logging
  • Provides a standard methodology for error handling
  • Makes your life much simpler

PowerCLI Script Template

Below is the full template which you can copy and save as a .ps1 file. Alternatively, there is a download link at the end of this article so that you don’t need to create your own .ps1 file.

Things to Note

The template is self-explanatory, however there are a few things I would like to point out:

  • This template uses my PowerShell Logging Function Library, which is a nice solution to create log files for all your scripts. If you are unfamiliar with my PowerShell Logging Function Library, then you can find out about it here.
  • $sScriptVersion – this is the variable that holds the current version of the script. This is written in the header of the log file by the Logging Function
  • Under the functions section, you will need to remove the <# and #> to uncomment the function. This is a template for a standard function. You can copy\paste this snippet of code for as many functions as you require. Generally, I create a separate function for each logical major steps of a script
  • Uncomment each of the lines in the Execution section and of course replace the “Script execution goes here” line with each of the functions you want to call and in the order you want to call them
  • The Connect-VMwareServer function is used to connect to your vCenter server or an ESXi host. To use it all you need to do is uncomment the following line under the Execution section of the template and then enter the IP or FQDN of your vCenter server or ESXi host:
    
    #Connect-VMwareServer -VMServer "Specify FQDN or IP of vCenter Server or Host"
    
    #Example to connect to vCenter in my test lab:
    Connect-VMwareServer -VMServer wp-vc-v01.testlab.com
    

Download Link

If you don’t want to copy the code above, then you can download the template from here: PowerCLI Script Template

If you get stuck or are unsure about anything, let me know in the comments below and I will help you out as much as I can. Also, if you have any new ideas or improvements let me know too!

Finally, if you haven’t already had experience with my PowerShell Script Template or PowerShell Script Logging Function, then I strongly suggest you take a look at them – they are very handy and can quickly improve your new and existing scripts.

Thanks everyone for the continued support.

Any questions or comments, then let me know below.

Luca

Comments

Leave a Comment

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