-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHobo.Log.psm1
142 lines (114 loc) · 3.19 KB
/
Hobo.Log.psm1
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
function Merge-LogPref
{
[cmdletbinding()]
param(
[parameter(Mandatory)]
[hashtable]$Pref,
[hashtable]$NewPref = @{}
)
process
{
if ($NewPref.Keys)
{
foreach ($key in $NewPref.Keys)
{
if ($Pref.ContainsKey($key))
{
$Pref[$key] = $NewPref[$key]
}
else
{
$Pref.Add($key, $NewPref[$key])
}
}
}
}
end
{
return $Pref
}
}
<#
.SYNOPSIS
This is a Powershell cmdlet to write output to the console, a log file, or the Windows Event Log with one command.
.DESCRIPTION
This cmdlet allows a user to write output to the console using their preferred output pipeline, as well as write the output to a file log and the Windows Event Log at the same time with one command.
.PARAMETER Message
The message to be logged
.PARAMETER LogType
The event type of the log entry being written. (INFO, WARN, ERROR)
.PARAMETER LogOutputPipeline
Choose to write to alternative pipelines (OUTPUT, INFO)
.PARAMETER LogOutputRaw
Writes a string to console output instead of a LogOutput object
.PARAMETER LogFile
Enables writing to the file log.
.PARAMETER LogFilePref
A hashtable of parameters to be sent to the Write-LogFile command.
.PARAMETER LogFileRaw
Writes a string to file output instead of a LogOutput object
.PARAMETER LogEvent
Enables writing to the Windows Event Log.
.PARAMETER LogEventPref
A hashtable of parameters to be sent to the Write-LogEvent command.
#>
function Write-LogTee
{
[cmdletbinding()]
param(
[parameter(Mandatory)]
[string]$Message,
[ValidateSet('INFO', 'WARN', 'ERROR')]
[string]$LogType = 'INFO',
[hashtable]$LogOutputPref = @{},
[ValidateSet('OUTPUT', 'INFO')]
[string]$LogOutputPipeline = 'OUTPUT',
[switch]$LogOutputRaw,
[switch]$LogFile,
[hashtable]$LogFilePref = @{},
[switch]$LogFileRaw,
[switch]$LogEvent,
[hashtable]$LogEventPref = @{}
)
begin
{
if ($LogOutputPipeline -ieq 'info')
{
$LogOutputRaw = $true
}
$LogOutputPref = Merge-LogPref -NewPref $LogOutputPrefs -Pref @{
LogType = $LogType
}
$LogEventPref = Merge-LogPref -NewPref $LogEventPref -Pref @{
LogType = $LogType
}
}
process
{
$outputObject = Write-LogOutput @LogOutputPref -Message $Message
if ($LogFile)
{
if ($LogFileRaw)
{
Write-LogFile @LogFilePref -InputObject $outputObject.ToRaw()
}
else
{
Write-LogFile @LogFilePref -InputObject $outputObject
}
}
if ($LogEvent)
{
Write-LogEvent @LogEventPref -Message $outputObject.ToRaw()
}
if ($LogOutputRaw)
{
$outputObject = $outputObject.ToRaw()
}
switch ($LogOutputPipeline)
{
'OUTPUT' { Write-Output $outputObject }
'INFO' { Write-Information $outputObject }
}
}
}