-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathWriteLogEntry.ps1
37 lines (35 loc) · 1.14 KB
/
WriteLogEntry.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
function Write-LogEntry {
[cmdletBinding()]
param (
[ValidateSet("Information", "Error")]
$Type = "Information",
[parameter(Mandatory = $true)]
$Message
)
switch ($Type) {
'Error' {
$Severity = 3
break;
}
'Information' {
$Severity = 6
break;
}
}
$DateTime = New-Object -ComObject WbemScripting.SWbemDateTime
$DateTime.SetVarDate($(Get-Date))
$UtcValue = $DateTime.Value
$UtcOffset = $UtcValue.Substring(21, $UtcValue.Length - 21)
$scriptname = (Get-PSCallStack)[1]
$logline = `
"<![LOG[$message]LOG]!>" + `
"<time=`"$(Get-Date -Format HH:mm:ss.fff)$($UtcOffset)`" " + `
"date=`"$(Get-Date -Format M-d-yyyy)`" " + `
"component=`"$($scriptname.Command)`" " + `
"context=`"$([System.Security.Principal.WindowsIdentity]::GetCurrent().Name)`" " + `
"type=`"$Severity`" " + `
"thread=`"$PID`" " + `
"file=`"$($Scriptname.ScriptName)`">";
$logline | Out-File -Append -Encoding utf8 -FilePath $Logfile -Force
Write-Verbose $Message
}