Renderers/EventLogRecordRenderer.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
function Write-EventLogHeader
{
    ## Do we need to write headers?
    if (($script:currentDirectory -eq ""))
    {
        Write-Host
        Write-Host "Time Created Id Level Provider Message"
        Write-Host "------------ ------ -------- -------- ----------------"

        $script:currentDirectory = "EventLog"
    }
}

function Write-EventLog
{
    param ([Parameter(Mandatory=$True,Position=1)] $item)

    Write-EventLogHeader

    $foreground = $global:PoshColor.EventLog.Default.Color    

    if ($item.Level -eq 1)
    {
        # Critical
        $foreground = $global:PoshColor.EventLog.Critical.Color
    }
    elseif ($item.Level -eq 2)
    {
        $foreground = $global:PoshColor.EventLog.Error.Color
    }
    elseif ($item.Level -eq 3)
    {
        $foreground = $global:PoshColor.EventLog.Warning.Color
    }
    elseif ($item.Level -eq 4)
    {
        $foreground = $global:PoshColor.EventLog.Information.Color
    }

    $providerName = $item.ProviderName.ToString()

    if ($providerName.Length -gt 20)
    {
        $providerName = $providerName.Substring(20)
    }

    $info = [String]::Format("{0,-22} {1, -7} {2, -12} {3, -20} {4}", $item.TimeCreated.ToString("G"), $item.Id, $item.LevelDisplayName, $providerName, $item.Message)

    Write-HostColor $info -Foreground $foreground

    return $true;
}