PowerShell: script template with help header and logging
PowerShell script skeleton (help, initialisation, functions, logging) based on a public gist.
A PowerShell script template with a help header, initialisations, declarations, functions and an execution section, based on a public gist (9to5IT/9620683). It is a starting point for administration scripts that write a log file.
Template layout
| Section | Content |
|---|---|
| Help header | Comment block with .SYNOPSIS, .DESCRIPTION, .PARAMETER, .INPUTS, .OUTPUTS, .NOTES, .EXAMPLE |
| Initialisations | $ErrorActionPreference and dot-sourcing of a logging function library |
| Declarations | Script version and log file path |
| Functions | Begin / Process / End skeleton with Try / Catch |
| Execution | Log start and finish calls around the main code |
Template
#requires -version 2
<#
.SYNOPSIS
<Overview of script>
.DESCRIPTION
<Brief description of script>
.PARAMETER <Parameter_Name>
<Brief description of parameter input required. Repeat this attribute if required>
.INPUTS
<Inputs if any, otherwise state None>
.OUTPUTS
<Outputs if any, otherwise state None>
.NOTES
Version: 1.0
Author: <Name>
Creation Date: <Date>
Purpose/Change: Initial script development
.EXAMPLE
<Example goes here. Repeat this attribute for more than one example>
#>
#---------------------------------------------------------[Initialisations]--------------------------------------------------------
#Set Error Action to Silently Continue
$ErrorActionPreference = "SilentlyContinue"
#Dot Source required Function Libraries
. "C:\Scripts\Functions\Logging_Functions.ps1"
#----------------------------------------------------------[Declarations]----------------------------------------------------------
#Script Version
$sScriptVersion = "1.0"
#Log File Info
$sLogPath = "C:\Windows\Temp"
$sLogName = "<script_name>.log"
$sLogFile = Join-Path -Path $sLogPath -ChildPath $sLogName
#-----------------------------------------------------------[Functions]------------------------------------------------------------
<#
Function <FunctionName>{
Param()
Begin{
Log-Write -LogPath $sLogFile -LineValue "<description of what is going on>..."
}
Process{
Try{
<code goes here>
}
Catch{
Log-Error -LogPath $sLogFile -ErrorDesc $_.Exception -ExitGracefully $True
Break
}
}
End{
If($?){
Log-Write -LogPath $sLogFile -LineValue "Completed Successfully."
Log-Write -LogPath $sLogFile -LineValue " "
}
}
}
#>
#-----------------------------------------------------------[Execution]------------------------------------------------------------
#Log-Start -LogPath $sLogPath -LogName $sLogName -ScriptVersion $sScriptVersion
#Script Execution goes here
#Log-Finish -LogPath $sLogFile
Notes
- The
Log-Start,Log-Write,Log-ErrorandLog-Finishfunctions come from theLogging_Functions.ps1library referenced by the template; it is not included here and must be adapted or replaced. $ErrorActionPreference = "SilentlyContinue"hides non-terminating errors: change it toStopif the script must halt on the first error.