OpenCloudConfig.psm1

<#
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
#>



function Write-Log {
  <#
  .SYNOPSIS
    Write a log message to the windows event log and powershell verbose stream
 
  .DESCRIPTION
    This function takes a log message and writes it to the windows event log as well as the powershell verbose stream.
    If the specified logName or source are missing from the windows event log, they are created.
 
  .PARAMETER message
    The message parameter is the log message to be recorded to the event log
 
  .PARAMETER severity
    The logging severity is the severity rating for the message being recorded.
    There are four ratings:
    - debug: verbose messages about state observations for debugging purposes
    - info: normal messages about state changes
    - warn: messages about unexpected occurences or observations that are not fatal to the running of the application
    - error: messages about failure of a critical logic path in the application
 
  .PARAMETER source
    The optional source parameter maps directly to the required event log source.
    This should be set to the name of the application being logged.
 
  .PARAMETER logName
    The optional logName parameter maps directly to the required event log logName.
    Most logs should go to the 'Application' pool
 
  .EXAMPLE
    These examples show how to call the Write-Log function with named parameters.
    PS C:\> Write-Log -message 'the sun is shining, the weather is sweet.' -severity 'debug' -source 'AmazingDaysApp'
    PS C:\> Write-Log -message 'it has started to rain. an umbrella has been provided.' -severity 'info' -source 'AmazingDaysApp'
    PS C:\> Write-Log -message 'thunder and lightning, very, very frightening.' -severity 'warn' -source 'AmazingDaysApp'
    PS C:\> Write-Log -message 'you are snowed in. the door is jammed shut.' -severity 'error' -source 'AmazingDaysApp'
 
  .NOTES
 
  #>

  [CmdletBinding()]
  param (
    [Parameter(Mandatory = $true)]
    [string] $message,

    [ValidateSet('debug', 'error', 'info', 'warn')]
    [string] $severity = 'info',

    [string] $source = 'OpenCloudConfig',
    [string] $logName = 'Application'
  )
  if ((-not ([System.Diagnostics.EventLog]::Exists($logName))) -or (-not ([System.Diagnostics.EventLog]::SourceExists($source)))) {
    New-EventLog -LogName $logName -Source $source
  }
  switch ($severity[0].ToString().ToLower()) {
    # debug
    'd' {
      $entryType = 'SuccessAudit'
      $eventId = 2
      break
    }
    # warn
    'w' {
      $entryType = 'Warning'
      $eventId = 3
      break
    }
    # error
    'e' {
      $entryType = 'Error'
      $eventId = 4
      break
    }
    # info
    default {
      $entryType = 'Information'
      $eventId = 1
      break
    }
  }
  Write-EventLog -LogName $logName -Source $source -EntryType $entryType -EventId $eventId -Message $message
  Write-Verbose -Message $message
}