Skip to content

Commit bddba58

Browse files
author
Antony Bragg
committed
Create Write-Log.psm1
Initial Commit.
0 parents  commit bddba58

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

Write-Log.psm1

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<#
2+
.SYNOPSIS
3+
Basic log file creator.
4+
5+
.DESCRIPTION
6+
Creates an entry of a specified severity into the log file.
7+
8+
.OUTPUTS
9+
A *.log file with specific name or location (Default is script root named log.log)
10+
11+
.PARAMETER Message
12+
The log message entered into the log file.
13+
14+
.PARAMETER severity
15+
The severity set for the log message within the log file. (Info, Warning, Error)
16+
17+
.PARAMETER logLocation
18+
The directory of the log file.
19+
20+
.PARAMETER logName
21+
The name of the log file. Should be a *.log extension. The default location is the area the script was executed.
22+
23+
.EXAMPLE
24+
write-log -Message "Oh no its all gone wrong"
25+
.EXAMPLE
26+
write-log -Message "Oh no its an error!" -severity "Error"
27+
.EXAMPLE
28+
write-log -Message "Oh no its all gone wrong" -severity "Error" -LogLocation "C:\temp\Log.log"
29+
30+
.NOTES
31+
This original module can be found in the following GitHub Repo: https://github.com/captainqwerty/Write-Log
32+
#>
33+
34+
Function Write-Log {
35+
Param(
36+
[Parameter(Position=0, Mandatory=$true, HelpMessage="Enter your log message.")]
37+
[string]$Message,
38+
39+
[Parameter(Position=1, HelpMessage="Enter the log level severity between.")]
40+
[ValidateSet("Info", "Warning", "Error")]
41+
[string]$severity = "Info",
42+
43+
[Parameter(Position=2, HelpMessage="Directory for the log file.")]
44+
[string]$logLocation = "$PSScriptRoot\log.log"
45+
)
46+
47+
if(!test-path $logLocation) {
48+
New-Item $logLocation -Force
49+
}
50+
51+
$timeStamp = Get-date -Format "dd/MM/yyyy HH:mm:ss"
52+
$Output = "$timeStamp - [$severity] $Message"
53+
Add-Content $logLocation -value $Output
54+
Write-Host "$Output"
55+
}
56+
57+
Export-ModuleMember -Function Write-Log

0 commit comments

Comments
 (0)