-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModule_Event_Logging.psm1
176 lines (148 loc) · 5.67 KB
/
Module_Event_Logging.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
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
<#
.Synopsis
Allows for Event Viewer and local file logging
.Description
Allows for multiple levels of Event Viewer logging. Also, there is built in file logging
which can be written to without adding to the Event Viewer; usually for mundane tasks
.Parameter PathToLogFile
The disc location to write the log file. Usually in the current users library somewhere.
.Parameter EventLogSourceName
What to name the log in the Event Viewer.
.Parameter EventLevel
The level of the event. Currently supports Info, Warn, Error
.Parameter WriteInEventViewer
A boolean (true, false) value for whether or not to write to the Event Viewer
.Parameter WriteFileLog
A boolean (true, false) value for whether or not to write a file log
.Parameter StringToLog
The string to write to the Event Viewer and/or the log file
.Example
# Basic ugly call.
Import-Module "\\SOMELOCATION\Event_Logger_Module.psm1" -Force
WriteLog "C:\User\Desktop" "Test log" "Info" $True "This is a test"
.Example
# More robust and pretty
Import-Module "\\SOMELOCATION\Event_Logger_Module.psm1" -Force
$myargs = @{
PathToLogFile = "C:\User\Desktop";
EventLogSourceName = "Test log";
WriteInEventViewer = $True
}
WriteLog @myargs -EventLevel:"Info" -StringToLog:"This is a test"
WriteLog @myargs -EventLevel:"Warn" -StringToLog:"This is a warn test"
.Notes
Version: 3
Author: userVII
Creation Date: 02-15-2017
Last Update: 06-07-2019
Can use -Verbose with Import-Module to verify it gets imported. Sometime script permissions interfere
Needs to run with Administrator rights on pc
#>
function Setup_FileLogging($logFilePath){
#Deletes log file if it gets too big
if (Test-Path $logFilePath){
$SizeMin = 10 #MB
if($logFilePath -Like "*.log" -Or $logFilePath -Like "*.txt"){
Get-ChildItem -Path $logFilePath | Where-Object { $_.Length / 1MB -gt $SizeMin } | Remove-Item -Force
}
}
#Create log file if doesn't exist
if (Test-Path $logFilePath){
Write-Verbose "Log file exists, writing output to that"
}elseif(!(Test-Path $logFilePath)) {
Write-Verbose "Creating log file"
New-Item $logFilePath -Force -ItemType File
}else{
Write-Warning "Something went wrong creating the log file"
}
}
function Setup_EventLogging($eventLogName, $eventSourceName){
#Create event log source if it doesn't exist
try{
if (! ([System.Diagnostics.EventLog]::SourceExists($eventSourceName))){
New-EventLog –LogName $eventLogName –Source $eventSourceName
}
}catch{
Write-Verbose "An error occured creating $eventSourceName in $eventLogName"
}
}
function Write_FileLog($logFilePath, $dateFormatted, $errorLevelString, $messageString){
if (Test-Path $logFilePath){
Add-content $logFilePath "($dateFormatted): $errorLevelString $messageString"
Add-content $logFilePath ""
}else{
Write-Warning "Could not write to log file"
}
}
function Write_EventViewerLog($eventLogName, $eventSourceName, $levelEntryTypeString, $levelValueInt, $errorLevelString, $messageString){
try{
if (([System.Diagnostics.EventLog]::SourceExists($eventSourceName))){
Write-EventLog -LogName $eventLogName –Source $eventSourceName –EntryType $levelEntryTypeString –EventID $levelValueInt –Message “$errorLevelString $messageString”
}else{
Write-Verbose "Could not write to Event Viewer"
}
}catch{
Write-Verbose "Could not write to $eventSourceName in $eventLogName, the event log may not exist."
}
}
function WriteLog(){
[CmdletBinding()]
param(
[Parameter(Mandatory=$false)]
[string]$PathToLogFile = "C:\General.Log",
[Parameter(Mandatory=$false)]
[string]$EventLogSourceName = "General Log",
[Parameter(Mandatory=$false)]
[string]$EventLevel = "Info",
[Parameter(Mandatory=$false)]
[switch]$WriteInEventViewer = $True,
[Parameter(Mandatory=$false)]
[switch]$WriteFileLog = $True,
[Parameter(Mandatory=$true)]
[string]$StringToLog,
[Parameter(Mandatory=$false)]
[switch]$EnableLogging = $True
)
$FormattedDate = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$LogName = "Custom Logs"
$LevelText = ""
$LevelValue = 0
$LevelEntryType = ""
#Setup for Event Viewer and log file
if($EnableLogging){
switch ($EventLevel){
"Error" {
Write-Verbose $StringToLog
$LevelText = "ERROR:"
$LevelValue = 3
$LevelEntryType = "Error"
break
}
"Warn" {
Write-Verbose $StringToLog
$LevelText = "WARNING:"
$LevelValue = 2
$LevelEntryType = "Warning"
break
}
"Info" {
Write-Verbose $StringToLog
$LevelText = "INFO:"
$LevelValue = 1
$LevelEntryType = "Information"
break
}
}
if($WriteFileLog){
Setup_FileLogging $PathToLogFile
Write_FileLog $PathToLogFile $FormattedDate $LevelText $StringToLog
}
if($WriteInEventViewer){
Setup_EventLogging $LogName $EventLogSourceName
Write_EventViewerLog $LogName $EventLogSourceName $LevelEntryType $LevelValue $LevelText $StringToLog
}
}else{
Write-Host $StringToLog
}
}
Export-ModuleMember -function WriteLog