Write-EventError.ps1

function Write-EventError
{
  <#
      .SYNOPSIS
      Add the text passed as a parameter into the $global:Output variable. Text added to has $global:Output a leading "!" to indicate that it is an error message
      If $globel:Debug is defined also writes the text into an event into the log defined in $global:EventLog with event source set as defined in $global:EventSource and EntryType as ERROR and EventID 666
      Get-StackIndent function is called to add some leading information about the script
      .EXAMPLE
      Write-EventError "Some text"
  #>

  param ([Parameter(Mandatory=$true)][String]$Message=$Global:nl)

  $Global:ErrorCount += 1
  $s = Get-StackIndent
  
  $global:Output += "!$s$message<br>"   + $Global:nl
  
  if ($global:debug)
  {
    Write-EventLog -LogName $global:EventLog -Source $global:EventSource -EntryType Error -EventID 666 -Message "!$s$message"
    "!$s$message" 
  }
}