Private/Notifiers/Invoke-NotifierEventLog.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
function Invoke-NotifierEventLog {
    [cmdletbinding()]
    param(
        [parameter(Mandatory, ValueFromPipeline)]
        [ValidateScript({ $_.PSObject.TypeNames[0] -eq 'Watchmen.Notifier.EventLog' })]
        [pscustomobject]$Notifier,

        [parameter(Mandatory)]
        [ValidateScript({ $_.PSObject.TypeNames[0] -eq 'OperationValidationResult' })]
        [pscustomobject]$Results
    )

    $o = ($Notifier | Format-Table -Property *  -AutoSize| Out-String)
    Write-Debug -Message "Event log notifier called with options:`n$o"

    # TODO
    # Even though we're specifying the 'Application' log, Write-EventLog is writing these events to the 'System' log.class Name
    # Need to investigate if this is a bug with PowerShell v5.1 or is happening on other PS versions.

    # Register 'Watchmen' event source if needed and create log entry
    $msg = @"
Watchmen reported a failure in OVF test:
 
Module: $($results.Module)
 
File: $($results.FileName)
 
Descibe: $($results.RawResult.Describe)
 
Context: $($results.RawResult.Context)
 
Test: $($results.RawResult.Name)
 
Result: $($results.Result)
 
Message: $($results.RawResult.FailureMessage)
 
Duration: $($results.RawResult.Time.ToString())
"@

    $params = @{
        LogName = 'Application'
        Source = 'Watchmen'
        EventId = $Notifier.EventId
        EntryType = $Notifier.EventType
        Message = $msg
    }
    New-EventLog -LogName $params.LogName -Source 'Watchmen' -ErrorAction Ignore
    Write-EventLog @params
}