Functions/Set-ScriptLogger.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<#
    .SYNOPSIS
        Update the script logger log configuration.
 
    .DESCRIPTION
        The script logger inside the current PowerShell session can be updated
        with all parameters inside this cmdlet.
 
    .INPUTS
        None.
 
    .OUTPUTS
        None.
 
    .EXAMPLE
        PS C:\> Set-ScriptLogger -Level 'Warning' -EventLog $true
        Set the script logger level to warning and enable the event log output.
 
    .EXAMPLE
        PS C:\> Set-ScriptLogger -Path 'C:\Temp\test.log' -Format '{3}: {4}'
        Update the log file path and its format.
 
    .NOTES
        Author : Claudio Spizzi
        License : MIT License
 
    .LINK
        https://github.com/claudiospizzi/ScriptLogger
#>


function Set-ScriptLogger
{
    [CmdletBinding(SupportsShouldProcess = $true)]
    param
    (
        # The logger name.
        [Parameter(Mandatory = $false)]
        [System.String]
        $Name = 'Default',

        # Update the path to the log file.
        [Parameter(Mandatory = $false)]
        [ValidateScript({Test-Path -Path (Split-Path -Path $_ -Parent)})]
        [System.String]
        $Path,

        # Update the format for the log file.
        [Parameter(Mandatory = $false)]
        [ValidateScript({$_ -f (Get-Date), $Env:ComputerName, $Env:Username, 'Verbose', 'My Message'})]
        [System.String]
        $Format,

        # Update the logger level.
        [Parameter(Mandatory = $false)]
        [ValidateSet('Verbose', 'Information', 'Warning', 'Error')]
        [System.String]
        $Level,

        # Update the used log file encoding.
        [Parameter(Mandatory = $false)]
        [ValidateSet('Unicode', 'UTF7', 'UTF8', 'UTF32', 'ASCII', 'BigEndianUnicode', 'Default', 'OEM')]
        [System.String]
        $Encoding,

        # Enable or disable the log file output.
        [Parameter(Mandatory = $false)]
        [System.Boolean]
        $LogFile,

        # Enable or disable the event log output.
        [Parameter(Mandatory = $false)]
        [System.Boolean]
        $EventLog,

        # Enable or disable the console output.
        [Parameter(Mandatory = $false)]
        [System.Boolean]
        $ConsoleOutput
    )

    if ($Script:Loggers.ContainsKey($Name))
    {
        if ($PSBoundParameters.ContainsKey('Path'))
        {
            # Create an empty log file, if it does not exist
            if (-not (Test-Path -Path $Path))
            {
                New-Item -Path $Path -ItemType File | Out-Null
            }

            # Only work with absolute path, makes error handling easier
            $Path = (Resolve-Path -Path $Path).Path

            if ($PSCmdlet.ShouldProcess('ScriptLogger.Path', 'Set'))
            {
                $Script:Loggers[$Name].Path = $Path
            }
        }

        if ($PSBoundParameters.ContainsKey('Format'))
        {
            if ($PSCmdlet.ShouldProcess('ScriptLogger.Format', 'Set'))
            {
                $Script:Loggers[$Name].Format = $Format
            }
        }

        if ($PSBoundParameters.ContainsKey('Level'))
        {
            if ($PSCmdlet.ShouldProcess('ScriptLogger.Level', 'Set'))
            {
                $Script:Loggers[$Name].Level = $Level
            }
        }

        if ($PSBoundParameters.ContainsKey('Encoding'))
        {
            if ($PSCmdlet.ShouldProcess('ScriptLogger.Encoding', 'Set'))
            {
                $Script:Loggers[$Name].Encoding = $Encoding
            }
        }

        if ($PSBoundParameters.ContainsKey('LogFile'))
        {
            if ($PSCmdlet.ShouldProcess('ScriptLogger.LogFile', 'Set'))
            {
                $Script:Loggers[$Name].LogFile = $LogFile
            }
        }

        if ($PSBoundParameters.ContainsKey('EventLog'))
        {
            if ($PSCmdlet.ShouldProcess('ScriptLogger.EventLog', 'Set'))
            {
                $Script:Loggers[$Name].EventLog = $EventLog
            }
        }

        if ($PSBoundParameters.ContainsKey('ConsoleOutput'))
        {
            if ($PSCmdlet.ShouldProcess('ScriptLogger.ConsoleOutput', 'Set'))
            {
                $Script:Loggers[$Name].ConsoleOutput = $ConsoleOutput
            }
        }
    }
}