Logging Functions

Here’s a collection of logging functions I came across that I’ve found quite useful.

Write-Log takes the string to be written, along with $logfile, $consoleoutput, and an $iserror switch. The time is identified. The message is then added to it, prepended with ERROR : if the iserror switch is passed.

function Write-Log([string[]]$Message, [string]$LogFile = $Script:LogFile, [bool]$ConsoleOutput = $Script:ConsoleOutput, [switch]$IsError)
{
$Message = $Message + $Input
if ($Message -ne $null -and $Message.Length -gt 0)
{
$TimeStamp = [System.DateTime]::Now.ToString("yyyy-MM-dd HH:mm:ss")
if ($LogFile -ne $null -and $LogFile -ne [System.String]::Empty)
{
Out-File -Append -FilePath $LogFile -InputObject "[$TimeStamp] $Message"
}
if ($ConsoleOutput -eq $true)
{
Write-Host "[$TimeStamp] $Message"
}
if ($IsError)
{
Write-Error "[$TimeStamp] ERROR: $Message"
exit -1
}


Set-ConsoleOutput
simply configures a script level variable. We use this variable to decide whether information logged to file is also output to the screen at the same time.

###############################################################################
# .SYNOPSIS
# Enable or disable console output through Write-Host when calling Write-Log.
# .DESCRIPTION
# Useful for calling PowerShell script when no console is available.
###############################################################################
function Set-ConsoleOutput([bool]$Enable = $true)
{
[bool]$Script:ConsoleOutput = $Enable
}

Start-Log begins the process of creating a file. If a file already exists, the overwrite parameter will determine what is done with this current file. When this is identified, the script level variable, $logfile is set.

###############################################################################
# .SYNOPSIS
# Start logging to the file name specified when Write-Log is called.
# Optional overwrite of existing log file.
###############################################################################
function Start-Log([string]$LogFile = $(Throw "Parameter LogFile must be specified."), [bool]$Overwrite = $true)
{
if ($Overwrite)
{
if (Test-Path -Path $LogFile)
{
Remove-Item -Path $LogFile
}
}
[string]$Script:LogFile = $LogFile
}

Stop-Log sets the value of the above script variable to null, which will result in an error the if we still attempt to use Write-Log

###############################################################################
# .SYNOPSIS
# Stop logging to file when Write-Log is called.
###############################################################################
function Stop-Log()
{
[string]$Script:LogFile = [System.String]::Empty
}

Get-Log returns the current value of the script level logfile variable

###############################################################################
# .SYNOPSIS
# Get the current log file used by Write-Log.
###############################################################################

function Get-Log()
{
return [string]$Script:LogFile
}

A very simple example of its use would be :

[string] $logdate = [System.DateTime]::Now.ToString('yyyy-MM-dd HHmmss')
[string] $logname = "Contoso Provisioning - $($logdate).log"
[string] $LogFile = "D:logs$logname"
[string] $scriptfile = $MyInvocation.MyCommand.Definition
Start-Log -LogFile $LogFile -Overwrite $true 
Write-Log -Message "Begun executing $scriptfile" -ConsoleOutput $False
Write-Log -Message "Configuring provisioning account" -ConsoleOutput $False
Write-Log -Message "Provisioning account credentials configured" -ConsoleOutput $False
Write-Log -Message "Creating secure string password" -ConsoleOutput $False
Write-Log -Message "Credential object created" -ConsoleOutput $False
Write-Log -Message "Azure Active Directory parameter is not set" -ConsoleOutput $False
Write-Log -Message "RDWEB URL is set to https://rdweb.contoso.local" -ConsoleOutput $False
Write-Log -Message "Multitenancy is not enabled" -ConsoleOutput $False
Write-Log -Message "Email subject is Provisioning setup" -ConsoleOutput $False
Write-Log -Message "Text body configured as HTML" -ConsoleOutput $False
Write-Log -Message "Execution of $scriptfile is complete" -ConsoleOutput $False
Stop-Log

Output wise, we then can get something like this :

[2015-12-31 14:22:53] Begun executing myfile.ps1
[2015-12-31 14:22:53] Configuring provisioning account
[2015-12-31 14:22:53] Provisioning account credentials configured
[2015-12-31 14:22:53] Creating secure string password
[2015-12-31 14:22:53] Credential object created
[2015-12-31 14:22:57] Azure Active Directory parameter is not set
[2015-12-31 14:22:57] RDWEB URL is set to https://rdweb.contoso.local
[2015-12-31 14:22:57] Multitenancy is not enabled
[2015-12-31 14:22:57] Email subject is Provisioning setup
[2015-12-31 14:22:58] Text body configured as HTML
[2015-12-31 14:22:53] Execution of myfile.ps1 is complete

Share

Leave a Reply

Your email address will not be published. Required fields are marked *