Private/Utils/Write-EventsToLog.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
function Write-EventsToLog {

    <#
    .SYNOPSIS
    Get logs from event.

    .DESCRIPTION
    Catches output from event for OutputDataSourceIdentifier (stdout) and ErrorDataSourceIdentifier (error)
    and writes proper logs.

    .OUTPUTS
    If event was generated for ErrorDataSourceIdentier then "StandardError" will be returned.

    .EXAMPLE
    Write-EventsToLog -OutputDataSourceIdentifier "ExternalProcessOutput" -ErrorDataSourceIdentifier "ExternalProcessError" -Output ([ref]$stdOut) -Quiet $false
    #>


    [CmdletBinding()]
    [OutputType([string])]
    param(
        # Event output data received source identifier.
        [Parameter(Mandatory=$true)]
        [string]
        $OutputDataSourceIdentifier,

        # Event error data received source identifier.
        [Parameter(Mandatory=$true)]
        [string]
        $ErrorDataSourceIdentifier,

        # Reference parameter with STDOUT text.
        [Parameter(Mandatory=$false)]
        [ref]
        $Output,

        # Reference parameter with STDERR text.
        [Parameter(Mandatory=$false)]
        [ref]
        $OutputStdErr,

        # If not null and given string will be present in stdout then "StringPresenceError" will be returned.
        [Parameter(Mandatory=$false)]
        [string]
        $FailOnStringPresence,

        # If true, no output from the command will be passed to the console.
        [Parameter(Mandatory=$true)]
        [bool]
        $Quiet,

        # Each stdout/stderr line that match this regex will be ignored (not written to console/$output).
        [Parameter(Mandatory=$false)]
        [string]
        $IgnoreOutputRegex
    )

    $error = ""
    # note: sometimes 'Collection was modified' is thrown by Get-Event.
    # Tried storing events in a hashset and removing them later in a loop, but it doesn't seem to help.
    # $eventIds = New-Object System.Collections.Generic.HashSet[System.String]

    try {
        Get-Event -SourceIdentifier $OutputDataSourceIdentifier -ErrorAction SilentlyContinue | ForEach-Object {
            if ($_.SourceEventArgs.Data -and (!$IgnoreOutputRegex -or $_.SourceEventArgs.Data -inotmatch $IgnoreOutputRegex)) {
                if (!$Quiet) {
                    Write-Log -Info ("[STDOUT] " + $_.SourceEventArgs.Data) -NoHeader
                }

                if ($FailOnStringPresence -and $_.SourceEventArgs.Data -imatch $FailOnStringPresence) {
                    $error = "StringPresenceError"
                }

                if ($Output) {
                    $Output.Value += $_.SourceEventArgs.Data
                }
            }
            Remove-Event -EventIdentifier $_.EventIdentifier -ErrorAction SilentlyContinue
            #[void]($eventIds.Add($_.EventIdentifier))
        }

        Get-Event -SourceIdentifier $ErrorDataSourceIdentifier -ErrorAction SilentlyContinue | ForEach-Object {
            if ($_.SourceEventArgs.Data -and (!$IgnoreOutputRegex -or $_.SourceEventArgs.Data -inotmatch $IgnoreOutputRegex)) {
                if (!$Quiet) {
                    Write-Log -Error ("[STDERR] " + $_.SourceEventArgs.Data) -NoHeader
                }
                $error = "StandardError"
                if ($OutputStdErr) {
                    $OutputStdErr.Value += $_.SourceEventArgs.Data
                }
            }
            Remove-Event -EventIdentifier $_.EventIdentifier -ErrorAction SilentlyContinue
            #[void]($eventIds.Add($_.EventIdentifier))
        }
    }
    catch {
      Write-Log -Warn ("Couldn't get events: {0}" -f $_)
    }

    <#try {
        # remove processed events from event queue
        foreach ($eventId in $eventIds) {
            Remove-Event -EventIdentifier $eventId -ErrorAction SilentlyContinue
        }
    } catch {
        Write-Log -Warn ("Couldn't remove event: {0}" -f $_)
    }#>


    return $error
}