, , ,

Since I often write scripts that run unattended, I always need to capture errors and then exit with a return code that tells me what went wrong.

In VBScript, I tend to have a block of constants at the top of my script mapping numeric codes to descriptive names.  This makes them easy to find and reference and easy to renumber without affecting the code itself.  Here’s a short example:

Const RC_SUCCESS = 0
Const RC_OBJECTS = 1

Here’s an example of how those might then be used.

Set objWshShell = CreateObject("WScript.Shell")
Set objWshNet = CreateObject("WScript.Network")
If Err.Number <> 0 Then WScript.Quit RC_OBJECTS

Today I’ve been working on a PowerShell script that will run regularly on a server as a scheduled task.  It was suggested I get it to write to an event log.  I needed a way to come up with a list of Event IDs and Messages to go with them so that I can refer to them in the code without losing track of them or their numbering.  Here’s a snippet of what I eventually came up with:

$eventIDs = @{
    "BEGIN_SCRIPT" = @(1, "Beginning execution of script: 'CantorisComputing.ps1'.");
    "FILE_MISSING" = @(2, "The file cannot be located at the specified path: '{0}'.");
    "COPY_SUCCESS" = @(3, "Copied folder '{0}' to the following target PC(s): '{1}'.")

Rather than a long list of constants, I’m using a hashtable where the key is the descriptive name for use in the script, and the paired value is a two item array consisting of a numeric Event ID and a string that holds the Event Message text. This means those two values can be pulled out by using the descriptive key name. Note the .NET formatting placeholders which make it easy to dynamically alter the exact message logged.
Here are some example uses of the above:

Write-EventLog -LogName $log -Source $src -EntryType Information  -EventId $eventIDs["BEGIN_SCRIPT"][0] -Message $eventIDs["BEGIN_SCRIPT"][1]
Write-EventLog -LogName $log -Source $src -EntryType Error -EventId $eventIDs["FILE_MISSING"][0] -Message ($eventIDs["FILE_MISSING"][1] -f $filePath)
Write-EventLog -LogName $log -Source $src -EntryType Information -EventId $eventIDs["COPY_SUCCESS"][0] -Message ($eventIDs["COPY_SUCCESS"][1] -f ($folder, ($hostnames -join ", ")))

The last example shows an array being inserted in as a comma-separated string.

So… over-engineered hell, or rather neat little trick? It certainly looks a bit clumsy to reference an event but I think it works rather well!

You can now read Part 2!