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:

  • 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 PowerShell 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 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.

Download Links

You can download the PowerShell 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.

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.

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.

Enjoy! Luca

Comments

  1. Thanks a lot for the template, it is really useful!
    Theres just one thing that I changed for me: I like to fold code that I’m not using right now with regions. So I changed the section headers:

    region ——————————Initialisations]—————————————

    endregion

  2. HI Luca,

    Continuing from what I have left in comments under- http://9to5it.com/powershell-script-template/

    The detailed summary of what I did-
    1) Installed PSLogging module in Powershell Modules directory.
    2) Modified this template, here I got confused on what should be function name whether to call function in PSLogging module or to insert script.
    3) I proceeded with addition of script and that delivered a horrendous errors
    Modified V2 template-

    $ErrorActionPreference = ‘SilentlyContinue’

    Import-Module PSLogging

    $sScriptVersion = ‘1.0’

    Log File Info

    $sLogPath = ‘C:\Temp’
    $sLogName = ‘.log’
    $sLogFile = Join-Path -Path $sLogPath -ChildPath $sLogName
    $Owner = Read-Host “Type in Distribution List”
    $User = Read-Host “Type in user to Add”

    Function Add-UserstoDL {
    Param ()
    Begin {
    Write-LogInfo -LogPath $sLogFile -Message ‘Generating Log’
    }
    Process {
    Try {
    $Owner = Read-Host “Type in Distribution List”
    $User = Read-Host “Type in user to Add”

    Add-DistributionGroupMember -Identity $Owner -Member $User

    }
    Catch {
    Write-LogError -LogPath $sLogFile -Message $_.Exception -ExitGracefully
    Break
    }

    }
    End {
    If ($?) {
    Write-LogInfo -LogPath $sLogFile -Message ‘Completed Successfully.’
    Write-LogInfo -LogPath $sLogFile -Message ‘ ‘
    }
    }
    }

    Start-Log -LogPath $sLogPath -LogName $sLogName -ScriptVersion $sScriptVersion
    Add-UserstoDL
    Stop-Log -LogPath $sLogFile

    Output-
    [PS] C:\PowerShell Scripts>.\Add-UserstoDL.ps1
    Type in Distribution List: Test XXXX
    Type in user to Add: abcd
    Test-Path : Illegal characters in path.
    At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\PSLogging\PSLogging.psm1:86 char:11
    + If ( (Test-Path -Path $sFullPath) ) {
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (C:\Temp\.log:String) [Test-Path], ArgumentException
    + FullyQualifiedErrorId : ItemExistsArgumentError,Microsoft.PowerShell.Commands.TestPathCommand

    New-Item : Illegal characters in path.
    At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\PSLogging\PSLogging.psm1:91 char:5
    + New-Item -Path $sFullPath -ItemType File
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [New-Item], ArgumentException
    + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.NewItemCommand

    Add-Content : Illegal characters in path.
    At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\PSLogging\PSLogging.psm1:93 char:5
    + Add-Content -Path $sFullPath -Value “*************************************** …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (C:\Temp\.log:String) [Add-Content], ArgumentException
    + FullyQualifiedErrorId : ItemExistsArgumentError,Microsoft.PowerShell.Commands.AddContentCommand

    Add-Content : Illegal characters in path.
    At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\PSLogging\PSLogging.psm1:93 char:5
    + Add-Content -Path $sFullPath -Value “*************************************** …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (C:\Temp\.log:String) [Add-Content], ArgumentException
    + FullyQualifiedErrorId : ItemExistsArgumentError,Microsoft.PowerShell.Commands.AddContentCommand

    Add-Content : Illegal characters in path.
    At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\PSLogging\PSLogging.psm1:93 char:5
    + Add-Content -Path $sFullPath -Value “*************************************** …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (C:\Temp\.log:String) [Add-Content], ArgumentException
    + FullyQualifiedErrorId : GetContentWriterArgumentError,Microsoft.PowerShell.Commands.AddContentCommand

    Add-Content : Illegal characters in path.
    At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\PSLogging\PSLogging.psm1:94 char:5
    + Add-Content -Path $sFullPath -Value “Started processing at [$([DateTime]::No …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (C:\Temp\.log:String) [Add-Content], ArgumentException
    + FullyQualifiedErrorId : ItemExistsArgumentError,Microsoft.PowerShell.Commands.AddContentCommand

    Add-Content : Illegal characters in path.
    At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\PSLogging\PSLogging.psm1:94 char:5
    + Add-Content -Path $sFullPath -Value “Started processing at [$([DateTime]::No …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (C:\Temp\.log:String) [Add-Content], ArgumentException
    + FullyQualifiedErrorId : ItemExistsArgumentError,Microsoft.PowerShell.Commands.AddContentCommand

    Add-Content : Illegal characters in path.
    At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\PSLogging\PSLogging.psm1:94 char:5
    + Add-Content -Path $sFullPath -Value “Started processing at [$([DateTime]::No …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (C:\Temp\.log:String) [Add-Content], ArgumentException
    + FullyQualifiedErrorId : GetContentWriterArgumentError,Microsoft.PowerShell.Commands.AddContentCommand

    Add-Content : Illegal characters in path.
    At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\PSLogging\PSLogging.psm1:95 char:5
    + Add-Content -Path $sFullPath -Value “*************************************** …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (C:\Temp\.log:String) [Add-Content], ArgumentException
    + FullyQualifiedErrorId : ItemExistsArgumentError,Microsoft.PowerShell.Commands.AddContentCommand

    Add-Content : Illegal characters in path.
    At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\PSLogging\PSLogging.psm1:95 char:5
    + Add-Content -Path $sFullPath -Value “*************************************** …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (C:\Temp\.log:String) [Add-Content], ArgumentException
    + FullyQualifiedErrorId : ItemExistsArgumentError,Microsoft.PowerShell.Commands.AddContentCommand

    Add-Content : Illegal characters in path.
    At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\PSLogging\PSLogging.psm1:95 char:5
    + Add-Content -Path $sFullPath -Value “*************************************** …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (C:\Temp\.log:String) [Add-Content], ArgumentException
    + FullyQualifiedErrorId : GetContentWriterArgumentError,Microsoft.PowerShell.Commands.AddContentCommand

    Add-Content : Illegal characters in path.
    At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\PSLogging\PSLogging.psm1:96 char:5
    + Add-Content -Path $sFullPath -Value “”
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (C:\Temp\.log:String) [Add-Content], ArgumentException
    + FullyQualifiedErrorId : ItemExistsArgumentError,Microsoft.PowerShell.Commands.AddContentCommand

    Add-Content : Illegal characters in path.
    At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\PSLogging\PSLogging.psm1:96 char:5
    + Add-Content -Path $sFullPath -Value “”
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (C:\Temp\.log:String) [Add-Content], ArgumentException
    + FullyQualifiedErrorId : ItemExistsArgumentError,Microsoft.PowerShell.Commands.AddContentCommand

    Add-Content : Illegal characters in path.
    At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\PSLogging\PSLogging.psm1:96 char:5
    + Add-Content -Path $sFullPath -Value “”
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (C:\Temp\.log:String) [Add-Content], ArgumentException
    + FullyQualifiedErrorId : GetContentWriterArgumentError,Microsoft.PowerShell.Commands.AddContentCommand

    Add-Content : Illegal characters in path.
    At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\PSLogging\PSLogging.psm1:97 char:5
    + Add-Content -Path $sFullPath -Value “Running script version [$ScriptVersion] …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (C:\Temp\.log:String) [Add-Content], ArgumentException
    + FullyQualifiedErrorId : ItemExistsArgumentError,Microsoft.PowerShell.Commands.AddContentCommand

    Add-Content : Illegal characters in path.
    At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\PSLogging\PSLogging.psm1:97 char:5
    + Add-Content -Path $sFullPath -Value “Running script version [$ScriptVersion] …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (C:\Temp\.log:String) [Add-Content], ArgumentException
    + FullyQualifiedErrorId : ItemExistsArgumentError,Microsoft.PowerShell.Commands.AddContentCommand

    Add-Content : Illegal characters in path.
    At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\PSLogging\PSLogging.psm1:97 char:5
    + Add-Content -Path $sFullPath -Value “Running script version [$ScriptVersion] …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (C:\Temp\.log:String) [Add-Content], ArgumentException
    + FullyQualifiedErrorId : GetContentWriterArgumentError,Microsoft.PowerShell.Commands.AddContentCommand

    Add-Content : Illegal characters in path.
    At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\PSLogging\PSLogging.psm1:98 char:5
    + Add-Content -Path $sFullPath -Value “”
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (C:\Temp\.log:String) [Add-Content], ArgumentException
    + FullyQualifiedErrorId : ItemExistsArgumentError,Microsoft.PowerShell.Commands.AddContentCommand

    Add-Content : Illegal characters in path.
    At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\PSLogging\PSLogging.psm1:98 char:5
    + Add-Content -Path $sFullPath -Value “”
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (C:\Temp\.log:String) [Add-Content], ArgumentException
    + FullyQualifiedErrorId : ItemExistsArgumentError,Microsoft.PowerShell.Commands.AddContentCommand

    Add-Content : Illegal characters in path.
    At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\PSLogging\PSLogging.psm1:98 char:5
    + Add-Content -Path $sFullPath -Value “”
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (C:\Temp\.log:String) [Add-Content], ArgumentException
    + FullyQualifiedErrorId : GetContentWriterArgumentError,Microsoft.PowerShell.Commands.AddContentCommand

    Add-Content : Illegal characters in path.
    At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\PSLogging\PSLogging.psm1:99 char:5
    + Add-Content -Path $sFullPath -Value “*************************************** …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (C:\Temp\.log:String) [Add-Content], ArgumentException
    + FullyQualifiedErrorId : ItemExistsArgumentError,Microsoft.PowerShell.Commands.AddContentCommand

    Add-Content : Illegal characters in path.
    At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\PSLogging\PSLogging.psm1:99 char:5
    + Add-Content -Path $sFullPath -Value “*************************************** …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (C:\Temp\.log:String) [Add-Content], ArgumentException
    + FullyQualifiedErrorId : ItemExistsArgumentError,Microsoft.PowerShell.Commands.AddContentCommand

    Add-Content : Illegal characters in path.
    At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\PSLogging\PSLogging.psm1:99 char:5
    + Add-Content -Path $sFullPath -Value “*************************************** …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (C:\Temp\.log:String) [Add-Content], ArgumentException
    + FullyQualifiedErrorId : GetContentWriterArgumentError,Microsoft.PowerShell.Commands.AddContentCommand

    Add-Content : Illegal characters in path.
    At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\PSLogging\PSLogging.psm1:100 char:5
    + Add-Content -Path $sFullPath -Value “”
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (C:\Temp\.log:String) [Add-Content], ArgumentException
    + FullyQualifiedErrorId : ItemExistsArgumentError,Microsoft.PowerShell.Commands.AddContentCommand

    Add-Content : Illegal characters in path.
    At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\PSLogging\PSLogging.psm1:100 char:5
    + Add-Content -Path $sFullPath -Value “”
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (C:\Temp\.log:String) [Add-Content], ArgumentException
    + FullyQualifiedErrorId : ItemExistsArgumentError,Microsoft.PowerShell.Commands.AddContentCommand

    Add-Content : Illegal characters in path.
    At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\PSLogging\PSLogging.psm1:100 char:5
    + Add-Content -Path $sFullPath -Value “”
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (C:\Temp\.log:String) [Add-Content], ArgumentException
    + FullyQualifiedErrorId : GetContentWriterArgumentError,Microsoft.PowerShell.Commands.AddContentCommand

    Add-Content : Illegal characters in path.
    At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\PSLogging\PSLogging.psm1:210 char:5
    + Add-Content -Path $LogPath -Value $Message
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (C:\Temp\.log:String) [Add-Content], ArgumentException
    + FullyQualifiedErrorId : ItemExistsArgumentError,Microsoft.PowerShell.Commands.AddContentCommand

    Add-Content : Illegal characters in path.
    At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\PSLogging\PSLogging.psm1:210 char:5
    + Add-Content -Path $LogPath -Value $Message
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (C:\Temp\.log:String) [Add-Content], ArgumentException
    + FullyQualifiedErrorId : ItemExistsArgumentError,Microsoft.PowerShell.Commands.AddContentCommand

    Add-Content : Illegal characters in path.
    At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\PSLogging\PSLogging.psm1:210 char:5
    + Add-Content -Path $LogPath -Value $Message
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (C:\Temp\.log:String) [Add-Content], ArgumentException
    + FullyQualifiedErrorId : GetContentWriterArgumentError,Microsoft.PowerShell.Commands.AddContentCommand

    Type in Distribution List:

    ——xxxxxxxxxxxxxx——–

    Luca, I think, must have missed lot of things, would appreciate your help in correcting me.

  3. Hi Luka,

    When I try to import-module pslogging I get the following error

    Import-Module : The specified module ‘PSLogging’ was not loaded because no valid module file was
    found in any module directory.
    At line:1 char:1
    + Import-Module PSLogging
    + ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : ResourceUnavailable: (PSLogging:String) [Import-Module], FileNotFoundE
    xception
    + FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleComma
    nd

  4. so this is logging the start time and it says running version then logs the end time but it does not log any details or weather is was successful or failed.

    1. Hi Tom, in order to get write information to the log you will need to comment out the function and then write your code within the process block. Alternatively, you can simply use Write-Log, Write-Error or Write-Warning cmdlets. Hope that helps Luca.

  5. Just had to comment to say thank you for this. I’ve been working to convert my old .bat and .vbs scripts to .ps1 scripts and very much need logging capability. The PSLogging module is a tremendous help, so thank you muchly!

    Two questions:
    1). I deploy the aforementioned scripts via SCCM. In order to use PSLogging, can I just copy the PSLogging folder to all PCs’ Modules folder, then import the module at the top of my scripts? Is it as simple as that?

    2.) The scripts that I have so far converted don’t follow your suggestion to build functions then call those functions later, but I very much like the idea. I’m still learning my way around PowerShell and would greatly appreciate a sample to go by as I convert my scripts. Would you mind including a working sample script along with the script templates?

    1. Hi Greg,

      Awesome, glad to here that it has been helpful. Here are the answers to your questions:

      1. Yes it is that simple. If you want to copy the module files to another location other than the default system PowerShell module location, then that is fine too. All you will need to do is add that location to the PSModulePath environmental variable on the machine. You can then simply run import-module PsLogging and it will work. You could also have the module on a file share accessible by all machines and add that path (although I have never tested this) however I can’t see why that wouldn’t work.

      2. Here is an example of where I am using multiple functions to break down the script into logical actions >>> How to Configure SNMP on an ESXi Host. Within the article, see Option 3.

      Hope this helps
      Luca

  6. When I copy the above template and insert my code into the Process Block (with Function commented out), it creates a log file but it does not execute the code nor does it write any errors. I’m also confused with the Execution block, we insert the Start-Log after the code? If I insert the code between those lines Start-Log and Stop-Log, the code is executed but nothing is logged as well. Please help.

    1. Hi Martin,

      Once you have created a function, you then need to call that function for it to be executed. You do that on line 89.

      If you are running Start-Log and Stop-Log then the minimum that should be completed by the script is the log file created and the initial start logging text and stop logging text be displayed within the log file. If this isn’t happening then maybe you haven’t set the log file path and name on lines 50 and 51 of the script template.

      Also, if you want to write information to the log then you will need to use the cmdlets Write-LogInfo, Write-LogWarning, or Write-LogError. Without calling those cmdlets then the log file will be empty (other than the text written by Start-Log and Stop-Log).

      Hope that helps
      Luca

  7. Hi there,
    Could you publish an actual code, (simple) that we can use as a template too? The template is good, but like to see actual samples, that would assist me in using this template.
    Thank you

  8. Hi, I need some help 🙂
    I am trying to get the files that are deleted to show up in log file. Here is the code, I am a newbie to Powershell
    Function convertedvideos {
    Param ($path0=”D:\ConvertedVideos”,$limit0= (Get-Date).AddDays(-5),$message0=””)
    Begin {
    Write-LogInfo -LogPath $sLogFile -Message ‘Deleting converted video files’
    }
    Process {
    Try {
    Get-ChildItem -Path $path0 -Force | Where-Object { !$.PSIsContainer -and $.LastWriteTime -lt $limit0 } | Remove-Item -Force -Recurse

    }
    Catch {
    Write-LogError -LogPath $sLogFile -Message $_.Exception -ExitGracefully
    Break
    }

    }
    End {
    If ($?) {

    Write-LogInfo -LogPath $sLogFile -Message 'Completed Successfully.'
    Write-LogInfo -LogPath $sLogFile -Message ' '

Leave a Comment