forked from tonypags/PsWinAdmin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWrite-Log.ps1
More file actions
111 lines (102 loc) · 3.21 KB
/
Copy pathWrite-Log.ps1
File metadata and controls
111 lines (102 loc) · 3.21 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
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
function Write-Log
{
<#
.Synopsis
Easily write to a log file within your scripts.
.DESCRIPTION
Allows a programmer to create consistant, organized
log entries. Outputs timestamp and tags for entry
type (ie: info, error)
.PARAMETER FilePath
Location of the log file.
.PARAMETER Content
The message contents to add to the log file.
.PARAMETER TimestampFormat
Define the datestamp string, defaults to 'yyyy\-MM\-dd\_HH\:mm\:ss'
.PARAMETER EntryType
Type of message, allowed are DEBUG, INFO*, WARN, ERROR, FAIL
.EXAMPLE
$LogSplat = @{
FilePath = 'C:\Temp\Log.txt'
TimestampFormat = 'yyyy\-MM\-dd\_HH\:mm\:ss'
EntryType = 'INFO'
}
[string]$var | log @LogSplat
.EXAMPLE
[string]$var | log @LogSplat -EntryType Error
.INPUTS
The message contents to add to the log file can be piped
into the function as a single string value.
#>
[CmdletBinding()]
[Alias('log')]
Param
(
# Location of the log file
[Parameter(Mandatory=$true,
Position=0)]
[ValidatePattern('.+\.log|.+\.txt')]
[string]
$FilePath,
# The message contents to add to the log file
[Parameter(Mandatory=$true,
ValueFromPipeline=$true,
Position=1)]
[Alias('Message')]
[string]
$Content,
# Define the datestamp string, defaults to DiDictionary
[string]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
$TimestampFormat = 'yyyy-MM-dd_HH:mm:ss',
# Type of message, allowed are DEBUG, INFO*, WARN, ERROR, FAIL, etc
[Parameter()]
[ValidateSet("DEBUG", "VERBOSE", "INFO", "WARN", "ERROR", "FAIL", "CRITICAL")]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
[string]
$EntryType = 'INFO'
)
Begin
{
}
Process
{
$EntryType = $EntryType.ToUpper()
# Make sure the file exists
if(Test-Path $FilePath){}else{
New-Item -ItemType File -Path $FilePath -Force |
Out-Null
}
# Build the string, starting with the date and type
[string]$strContent = $null
$strContent = $strContent + "$((Get-Date).ToString($TimestampFormat)) "
$strContent = $strContent + "[$($EntryType)]: "
$strContent = $strContent + $env:USERNAME + '@'
$strContent = $strContent + $env:COMPUTERNAME + '.'
$strContent = $strContent + $env:USERDNSDOMAIN + ' '
$strContent = $strContent + $Content
# Add Content to the file.
Try{
$Splat = @{
Value = $strContent
Path = $FilePath
Force = $true
ErrorAction = 'Stop'
ErrorVariable = 'LogError'
}
Add-Content @Splat
}
Catch{
if(!$WriteLogErrorHappened){
Write-Host "Logging error:" -ForegroundColor Gray
Write-Host "$($LogError.ErrorRecord)`n" -ForegroundColor DarkCyan
Set-Variable -Name 'WriteLogErrorHappened' -Scope Global -Value $true
}
}
}
End
{
}
}