Monday, January 16, 2012

Powershell (v2) - Simplify Logging with Functions to Save Typing

As I work on my current project, I like to include comments at the beginning and end of each section.  When I started off my scripts, they looked like this:
#region Parameters

param(
[Parameter(
HelpMessage = 'You need to enter a path to store the transcript.',
Mandatory = $true
)]
[ValidateNotNullOrEmpty()]
[String]
$TranscriptPath
)

#endregion Parameters

#region Functions

function fuwdt {
Get-Date -Format "HH:mm:ss"
}

#endregion Functions

#region ScriptConfiguration

#requires -Version 2.0
#requires -PSSnapIn PSCX
#requires -PSSnapIn ServerManager
#requires -PSSnapIn WebAdministration
Set-StrictMode -Version 2.0
Start-Transcript -Path $TranscriptPath

#endregion ScriptConfiguration

#region ScriptBody

#region Log Header

# Output status to host
Write-Output ""
Write-Output "$('*' * 50)"
Write-Output "* Beginning application configuration (using script: $(Join-Path $(Split-Path -parent $MyInvocation.MyCommand.Definition) $(Split-Path -Leaf $MyInvocation.MyCommand.Definition)))."
Write-Output "* Script run on $(Get-Date)."
Write-Output "$('*' * 50)"
Write-Output ""

#endregion Log Header

#region Step 1) Create application pools

#### NOTES: #################################
#
# This step will create AppPools based on the configuration for dev.
#
#############################################

# Output status to host
Write-Output "$(fuwdt): $('*' * 75)"
Write-Output "$(fuwdt): * Step 1) Creating AppPools."
Write-Output "$(fuwdt): $('*' * 75)"
Write-Output "$(fuwdt):"

... Do some work ...

# Output status to host
Write-Output "$(fuwdt):"
Write-Output "$(fuwdt): $('*' * 50)"
Write-Output "$(fuwdt): * AppPools created."
Write-Output "$(fuwdt): $('*' * 50)"
Write-Output "$(fuwdt):"

#endregion Step 1) Create application pools
So, this is all well and good, but, when I started having several sections to my scripts, I was repeating this pattern over and over,
# Output status to host
Write-Output "$(fuwdt): $('*' * 75)"
Write-Output "$(fuwdt): * Step 1) Creating AppPools."
Write-Output "$(fuwdt): $('*' * 75)"
Write-Output "$(fuwdt):"

... Do some work ...

# Output status to host
Write-Output "$(fuwdt):"
Write-Output "$(fuwdt): $('*' * 50)"
Write-Output "$(fuwdt): * AppPools created."
Write-Output "$(fuwdt): $('*' * 50)"
Write-Output "$(fuwdt):"
Kind of foolish I figured, so, I wrote these two functions, and, put them in a base.psm1 module. You could also include them in the Functions region.
function Close-Comments {
param(
$Comment
)

Write-Output "$(fuwdt):"
Write-Output "$(fuwdt): $('*' * 50)"
Write-Output "$(fuwdt): * $($Comment)."
Write-Output "$(fuwdt): $('*' * 50)"
Write-Output "$(fuwdt):"
}

function Open-Comments {
param(
$Comment
)

Write-Output "$(fuwdt): $('*' * 75)"
Write-Output "$(fuwdt): * $($Comment)."
Write-Output "$(fuwdt): $('*' * 75)"
Write-Output "$(fuwdt):"
}
With these two functions, I can rewrite my block this way,
# Output status to host
Open-Comments "Step 1) Creating AppPools."

... Do some work ...

# Output status to host
Close-Comments "AppPools created."
Quite a bit less typing, and, definitely cuts down on redundant code.

0 comments:

Post a Comment