-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWrite-Log.ps1
131 lines (104 loc) · 4.1 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
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
function Write-Log {
<#
.SYNOPSIS
Write to a Log File
.DESCRIPTION
Write to a Log File
.NOTES
File-Name: write-Log.ps1
Author: Josh Burkard - [email protected]
Version: 1.0.0
Changelog:
1.0.0, 2020-04-23, Josh Burkard, initial creation
.PARAMETER Status
the status of the message.
this string parameter is mandatory and allows one off this values:
'INFO', 'WARN', 'ERROR', 'VERBOSE', 'OK', 'END', 'START'
.PARAMETER Message
the message to display
this string parameter is mandatory
.PARAMETER LogName
the file of the log
this string parameter is mandatory, it can be submitted through $PSDefaultParameterValues
.PARAMETER SubStepLevel
the level of the indentation
default is 0
this integer parameter is not mandatory
.PARAMETER NoOutput
if this switch parameter is set, there will be no console output. the logged message will only be writen to the logfile
.LINK
https://github.com/joshburkard/___PowerShell-Functions
.EXAMPLE
$PSDefaultParameterValues = @{}
$PSDefaultParameterValues.Add( "Write-Log:LogName", "c:\Admin\Logs\$Package-$Program-$( Get-Date -Format "yyyyMMdd-HHmmss" ).log" )
Write-Log -Message "Test Message" -Status INFO
Write-Log -Message "Test Message 1" -Status OK -SubStepLevel 1
#>
[CmdletBinding()]
param (
[parameter(Mandatory=$true)]
[ValidateSet('INFO', 'WARN', 'ERROR', 'VERBOSE', 'OK', 'END', 'START')]
[Alias('Severity')]
[string]
$Status,
[parameter(Mandatory=$true)]
[string]
$Message,
[string]
$LogName = "c:\Admin\Logs\OSDeployment.log"
,
[switch]$NoOutput
,
[switch]$NoLog
,
[Parameter(Mandatory=$false)]
[int]$SubStepLevel = 0
)
if ( $NoLog -eq $false ) {
$LogFile = $LogName
$Path = Split-Path -Path $LogFile
if (!(Test-Path -Path "$Path")) {
[void]( New-Item -Type directory -Path "$Path" -Force )
}
if( -not ( Test-Path -Path $LogFile ) ) {
[void]( New-Item -Path $LogFile -ItemType File )
}
$WriteSuccess = $false
$LogMessage = $Message -Replace "((\033\[)(\d*)m)", ''
$retry = 0
do {
try {
$retry++
$stream = [System.IO.StreamWriter]::new($LogFile, $true, ([System.Text.Utf8Encoding]::new()))
$stream.WriteLine( "$( ( [System.DateTime]::Now ).ToString() ) $( $Status.PadRight(8, ' ').ToUpper() ) - $( ''.PadRight( ($SubStepLevel * 2) , ' ') )$LogMessage" )
$stream.close()
$WriteSuccess = $true
}
catch {
# Write-Host "." -ForegroundColor Yellow -NoNewline
Start-Sleep -Milliseconds 10
}
} until ( ( $WriteSuccess -eq $true ) -or ( $retry -ge 5 ) )
if ( $WriteSuccess -eq $false ) {
try {
"$( ( [System.DateTime]::Now ).ToString() ) $( $Status.PadRight(8, ' ').ToUpper() ) - $( ''.PadRight( ($SubStepLevel * 2) , ' ') )$LogMessage" | Out-File -FilePath $LogFile -Encoding utf8 -Append
$WriteSuccess = $true
}
catch {
Write-Host "couldn't write to log" -ForegroundColor Red
}
}
}
Switch ($Status) {
'Info' {$FColor='gray'}
'Warning' {$FColor='yellow'}
'WARN' {$FColor='yellow'}
'Error' {$FColor='red'}
'Verbose' {$FColor='yellow'}
'Ok' {$FColor='green'}
Default {$FColor='gray'}
}
if ( $NoOutput -eq $false ) {
Write-Host "$(([System.DateTime]::Now).ToString()) [$($Status.PadRight(8, ' ').ToUpper())] - $( ''.PadRight( ($SubStepLevel * 2) , ' ') )$($Message)" -ForegroundColor $FColor
}
}