Functions/Get-PARComponentLog.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
Function Get-PARComponentLog {
    <#
    .SYNOPSIS
    Returns events from remote component log.

    .DESCRIPTION
    Gets events from logs for remote Vault, PADR, CVM or ENE.

    .PARAMETER Server
    The name or address of the remote Vault server to target with PARClient

    .PARAMETER Password
    The password for remote operations via PARClient as a secure string

    .PARAMETER Credential
    The password for remote operations via PARClient held in a credential object

    .PARAMETER PassFile
    The path to a "password" file created by PARClient.exe, containing the encrypted password value used for remote
    operations via PARClient

    .PARAMETER Component
    The name of the component to query. Vault, PADR, CVM or ENE are the accepted values

    .PARAMETER LogFile
    For ENE & CVM Logs, the log file to return must be specified. Accepted values are "Console" or "Trace"

    .PARAMETER TimeFrom
    For Vault or PADR log queries, optionally provide a datetime from which to return the logs from.

    .PARAMETER Lines
    For Vault log queries, optionally provide the number of log lines to return.

    .EXAMPLE
    Get-PARComponentLog -Server EPV1 -Password $SecureString -Component Vault -TimeFrom (Get-Date).AddDays(-1)

    Show events from the ITALOG file on Vault EPV1 from the last 24 hours

    .EXAMPLE
    Get-PARComponentLog -Server EPV1 -Password $SecureString -Component ENE -LogFile Trace

    Show events from the ENE Trace log on vault EPV1

    .EXAMPLE
    Get-PARComponentLog -Server EPV1 -Password $SecureString -Component Vault -Lines 10

    Show the last 10 lines of the ITALOG file on vault EPV1

    #>

    [CmdletBinding()]
    Param(
        [Parameter(
            Mandatory = $true,
            ValueFromPipelineByPropertyName = $true
        )]
        [string]$Server,

        [Parameter(
            Mandatory = $true,
            ValueFromPipelineByPropertyName = $true,
            ParameterSetName = "Password"
        )]
        [securestring]$Password,

        [Parameter(
            Mandatory = $true,
            ValueFromPipelineByPropertyName = $true,
            ParameterSetName = "Credential"
        )]
        [pscredential]$Credential,

        [Parameter(
            Mandatory = $True,
            ValueFromPipelineByPropertyName = $True,
            ParameterSetName = "PassFile"
        )]
        [ValidateScript( {Test-Path $_ -PathType Leaf})]
        [string]$PassFile,

        [Parameter(
            Mandatory = $true,
            ValueFromPipelineByPropertyName = $true
        )]
        [ValidateSet("Vault", "PADR", "ENE", "CVM")]
        [string]$Component,

        [Parameter(
            Mandatory = $false,
            ValueFromPipelineByPropertyName = $true
        )]
        [ValidateSet("Console", "Trace")]
        [string]$LogFile = "Console",

        [Parameter(
            Mandatory = $false,
            ValueFromPipelineByPropertyName = $true
        )]
        [datetime]$TimeFrom,

        [Parameter(
            Mandatory = $false,
            ValueFromPipelineByPropertyName = $true
        )]
        [int]$Lines
    )

    Begin {

        $PADR = '^\[(\d+\/\d+\/\d+\s+\d+\:\d+\:\d+\.\d+)\]\W+([A-Z]+\d+[A-Z](?:\s))?(.+)$'
        $Vault = '^(\d+\/\d+\/\d+ \d+:\d+:\d+) ([A-Z]+[0-9]+[A-Z]) (.+)$'
        $ENEConsole = '^\[(\d+\/\d+\/\d+\s\W\s\d+\:\d+\:\d+)\]\W+([A-Z]+\d+[A-Z])\s(.+)$'
        $ENETrace = '^\[(\d+\/\d+\/\d+(?:\s\W)\s\d+\:\d+\:\d+)\.\d+\].+\|\s([A-Z]+(?:[A-Z]|\d)[A-Z]\d+[A-Z](?:\s))?(.+)$'

    }

    Process {

        $Command = "GetLog $Component"

        if(($Component -eq "VAULT") -or ($Component -eq "PADR")) {

            if($PSBoundParameters.ContainsKey("TimeFrom")) {

                $DateStamp = (Get-Date $($PSBoundParameters["TimeFrom"]) -Format ddMMyyyy:HHmm)
                $Command = "$Command /TimeFrom $DateStamp"

            }

        }

        if($Component -eq "VAULT") {

            if($PSBoundParameters.ContainsKey("Lines")) {

                $Command = "$Command /Lines $($PSBoundParameters["Lines"])"

            }

        }

        if(($Component -eq "ENE") -or ($Component -eq "CVM")) {

            $Command = "$Command /LogFile $($PSBoundParameters["LogFile"])"

        }

        $PSBoundParameters.Add("CommandParameters", "$Command")

        switch ($Component) {

            "PADR" {$Pattern = $PADR; break}

            "Vault" {$Pattern = $Vault; break}

            "ENE" {

                if ($($PSBoundParameters["LogFile"]) -eq "Console") {

                    $Pattern = $ENEConsole; break

                } elseif ($($PSBoundParameters["LogFile"]) -eq "Trace") {

                    $Pattern = $ENETrace; break

                }

            }

            default {$Pattern = '(.+)'; break}

        }

        $Result = Invoke-PARClient @PSBoundParameters

        If($Result.StdOut) {

            ($Result.StdOut).Split("`n") | ForEach-Object {

                $event = ($_ | Select-String $Pattern -AllMatches)

                if($event -match '\S') {

                    [PSCustomObject]@{

                        "Time"    = $event.Matches.Groups[1].Value -replace '(\s\W\s)', ' '
                        "Code"    = $event.Matches.Groups[2].Value
                        "Message" = $event.Matches.Groups[3].Value

                    } | Add-ObjectDetail -typename VaultControl.Log.Component

                }

            }

        }

    }

}