-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWrite-Log.ps1
More file actions
66 lines (50 loc) · 1.63 KB
/
Copy pathWrite-Log.ps1
File metadata and controls
66 lines (50 loc) · 1.63 KB
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
<#PSScriptInfo
.DESCRIPTION A Simple logging function to incorporate in to scripts and automations
.VERSION 1.0.0.0
.GUID 8967535b-7bf2-471d-ad6a-b0802b513ed4
.AUTHOR Tom Stryhn
.COMPANYNAME Tom Stryhn
.COPYRIGHT 2023 (c) Tom Stryhn
.TAGS PowerShell Log LogEvent Write
.LICENSEURI https://github.com/tomstryhn/PowerShell/blob/main/LICENSE
.PROJECTURI https://github.com/tomstryhn/PowerShell/Universal/Write-Log/
#>
function Write-Log {
[CmdletBinding()]
param (
# The Message to be written to the LogFile
[Parameter(
Position = 0,
Mandatory = $true,
ValueFromPipeline = $true
)]
[string]
$Message,
# The Path to the actual LogFile
[Parameter(
Mandatory = $true,
Position = 1
)]
[ValidateScript(
{
if ($_){ Test-Path $_}
})]
[string]
$LogPath,
# The Log Type, which can be either Information, Warning or an Error
[Parameter(
Position = 2
)]
[ValidateSet('Information', 'Warning', 'Error')]
[string]
$LogLevel = 'Information'
)
process {
# TimeStamp according the ISO 8601 Format
$timeStamp = Get-Date -Format 'yyyy-MM-dd HH:mm:ss.fff'
# The String to be added for the Log
$logEntry = $timeStamp + "`t" + $LogLevel + "`t" + $Message
# Writing the LogFile
$logEntry | Out-File -Append -FilePath $LogPath
}
}