-
Notifications
You must be signed in to change notification settings - Fork 61
/
Write-JVLog.ps1
68 lines (57 loc) · 2.11 KB
/
Write-JVLog.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
function Write-JVLog {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, Position = 0)]
[String]$Message,
[Parameter(Mandatory = $true, Position = 1)]
[ValidateSet('Debug', 'Info', 'Warning', 'Error')]
[String]$Level,
[Parameter()]
[AllowEmptyString()]
[String]$Write,
[Parameter()]
[AllowEmptyString()]
[String]$WriteLevel,
[Parameter()]
[AllowEmptyString()]
[String]$LogPath,
[Parameter()]
[ValidateSet('Break', 'Continue', 'Ignore', 'Inquire', 'SilentlyContinue', 'Stop', 'Suspend')]
[String]$Action = 'Stop'
)
$timeStamp = Get-Date -Format s
if ($Level -eq 'Debug') {
if ($WriteLevel -eq 'Debug') {
$formattedMessage = "[$timeStamp][DEBUG] $Message"
}
Write-Debug -Message "$Message"
}
if ($Level -eq 'Info') {
if ($WriteLevel -eq 'Debug' -or $WriteLevel -eq 'Info') {
$formattedMessage = "[$timeStamp][INFO ] $Message"
}
Write-Verbose -Message $Message
}
if ($Level -eq 'Warning') {
if ($WriteLevel -eq 'Debug' -or $WriteLevel -eq 'Info' -or $WriteLevel -eq 'Warning') {
$formattedMessage = "[$timeStamp][WARN ] $Message"
}
Write-Warning -Message $Message
}
if ($Level -eq 'Error') {
if ($writeLevel -eq 'Debug' -or $WriteLevel -eq 'Info' -or $WriteLevel -eq 'Warning' -or $WriteLevel -eq 'Error') {
$formattedMessage = "[$timeStamp][ERROR] $Message"
}
Write-Error -Message $Message -ErrorAction $Action
}
if ($LogPath -ne '' -and $null -ne $LogPath) {
if ($formattedMessage -ne '' -and $null -ne $formattedMessage) {
if ($Write -eq 1) {
$LogMutex = New-Object System.Threading.Mutex($false, "LogMutex")
$LogMutex.WaitOne() | Out-Null
$formattedMessage | Out-File -FilePath $LogPath -Append
$LogMutex.ReleaseMutex() | Out-Null
}
}
}
}