The other day I posted a quick primer on logging in our PS scripts. Here I will expand on that, with a use-case example I just wrote code for yesterday. Here is a registry-reading function I bashed together, as but one example of something I needed to accomplish in reading in a setting and logging it. Thus allowing me to keep a thorough, auditable record of registry changes during an automation task.
Reading Registry Values and Logging Them
For many automation tasks, you’ll need to retrieve configurations from the Windows Registry. The Get-RegistryValue function allows you to read a specific property from the Registry without cluttering your code. Errors are cleanly handled (returning $false if a value can’t be found), making it easy to integrate error-checking and logging.
|
|
And here’s a recap of the Writelog function I wrote about the other day:
|
|
Example Usage:
|
|
You should log everything you’re reading-in, or changes you’re making (files written, etc) during any scheduled or automated run of your PS scripts. This example shows a combination of Get-RegistryValue and WriteLog function to demonstrate a use-case, and the benefit becomes obvious once you make this a coding practice: you can retrieve configurations, make decisions based on those values, and log both the successes and failures in a neatly timestamped manner.
Happy scripting…