-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathWrite-Log.ps1
49 lines (39 loc) · 1.43 KB
/
Write-Log.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
Function Write-Log
{
<#
.SYNOPSIS
A function to write ouput messages to a logfile.
.DESCRIPTION
This function is designed to send timestamped messages to a logfile of your choosing.
Use it to replace something like write-host for a more long term log.
.PARAMETER StrMessage
The message being written to the log file.
.PARAMETER Severity
The label assigned to that log message line. Options are "Note", "Warning", and "Problem"
.EXAMPLE
PS C:\> Write-Log -StrMessage 'This is a note message being written out to the log.' -Severity 1
PS C:\> Write-Log -StrMessage 'This is a warning message being written out to the log.' -Severity 2
PS C:\> Write-Log -StrMessage 'This is a error message being written out to the log.' -Severity 3
PS C:\> Write-Log -StrMessage 'This message being written has no severity.'
.NOTES
N/A
#>
Param
(
[Parameter(Mandatory = $True, Position = 0)]
[String]$Message,
[Parameter(Mandatory = $False, Position = 1)]
[INT]$Severity
)
$Note = "[NOTE]"
$Warning = "[WARNING]"
$Problem = "[ERROR]"
[string]$Date = get-date
switch ($Severity)
{
1 { add-content -path $LogFilePath -value ($Date + "`t:`t" + $Note + $Message) }
2 { add-content -path $LogFilePath -value ($Date + "`t:`t" + $Warning + $Message) }
3 { add-content -path $LogFilePath -value ($Date + "`t:`t" + $Problem + $Message) }
default { add-content -path $LogFilePath -value ($Date + "`t:`t" + $Message) }
}
}