This repository has been archived by the owner on May 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Write-Log.ps1
60 lines (52 loc) · 1.63 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
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT license.
# --------------------------------------------------------------------------------------------
[System.IO.FileInfo]$Script:LTI_LOG_FILE = $null
function Write-LogInternal {
param (
[Parameter(Mandatory)]
[String]$Message
)
$now = (Get-Date).ToString()
$logMsg = "[ $now ] - $Message"
if( $Script:LTI_LOG_FILE ) {
$logMsg >> $Script:LTI_LOG_FILE
# Print the output on the screen, if running verbose
Write-Verbose -Message $logMsg
}
else {
# No LogFile set, print output on screen instead
Write-Host $logMsg
}
}
function Set-LogFile {
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[System.IO.FileInfo]$Path
)
process {
# We'd append the logs to this File
$Script:LTI_LOG_FILE = $Path
}
}
function Write-Log {
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[string]$Message,
$ErrorRecord
)
process {
# credits: Tim Curwick for providing this insight
if( $ErrorRecord -is [System.Management.Automation.ErrorRecord] ) {
Write-LogInternal "[ERROR] - Exception.Message [ $($ErrorRecord.Exception.Message) ]"
Write-LogInternal "[ERROR] - Exception.Type [ $($ErrorRecord.Exception.GetType()) ]"
Write-LogInternal "[ERROR] - $Message"
}
else {
Write-LogInternal "[INFO] - $Message"
}
}
}