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.

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.

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.

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

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

A very simple example of its use would be :

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

Leave a Reply

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