Skip to content

Commit bc86b08

Browse files
committed
Initial commit
1 parent 27d1c9a commit bc86b08

26 files changed

+1394
-1
lines changed

Examples/LoggerDemo.ps1

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
# Initialize the logger with default values
3+
Start-ScriptLogger
4+
5+
# Alternative: Specify all possible parameters for the logger
6+
Start-ScriptLogger -Path 'C:\Temp\test.log' -Format '{0:yyyy-MM-dd} {0:HH:mm:ss} {1} {2} {3,-11} {4}' -Level 'Verbose' -SkipEventLog -HideConsoleOutput
7+
8+
# Log an error message
9+
Write-ErrorLog -Message 'My Error Message'
10+
11+
# Log a warning massage
12+
Write-WarningLog -Message 'My Warning Message'
13+
14+
# Log an information message
15+
Write-InformationLog -Message 'My Information Message'
16+
17+
# Log a verbose message
18+
Write-VerboseLog -Message 'My Verbose Message'
19+
20+
# Disable the logger
21+
Stop-ScriptLogger

Functions/Get-ScriptLogger.ps1

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<#
2+
.SYNOPSIS
3+
4+
5+
.DESCRIPTION
6+
7+
8+
.EXAMPLE
9+
C:\>
10+
11+
.EXAMPLE
12+
C:\>
13+
#>
14+
15+
function Get-ScriptLogger
16+
{
17+
[CmdletBinding()]
18+
param
19+
(
20+
)
21+
22+
if ($Global:ScriptLogger -ne $null)
23+
{
24+
return $Global:ScriptLogger
25+
}
26+
else
27+
{
28+
throw 'Script logger not found!'
29+
}
30+
}

Functions/Set-ScriptLogger.ps1

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<#
2+
.SYNOPSIS
3+
4+
5+
.DESCRIPTION
6+
7+
8+
.PARAMETER Path
9+
10+
11+
.PARAMETER Format
12+
13+
14+
.PARAMETER Level
15+
16+
17+
.PARAMETER Enabled
18+
19+
20+
.EXAMPLE
21+
C:\>
22+
23+
.EXAMPLE
24+
C:\>
25+
#>
26+
27+
function Set-ScriptLogger
28+
{
29+
[CmdletBinding()]
30+
param
31+
(
32+
[Parameter(Position=0,
33+
Mandatory=$false)]
34+
[ValidateScript({Test-Path -Path (Split-Path -Path $_ -Parent)})]
35+
[String] $Path,
36+
37+
[Parameter(Position=1,
38+
Mandatory=$false)]
39+
[ValidateScript({$_ -f (Get-Date), $Env:ComputerName, $Env:Username, 'Verbose', 'My Message'})]
40+
[String] $Format,
41+
42+
[Parameter(Position=2,
43+
Mandatory=$false)]
44+
[ValidateSet('Verbose', 'Information', 'Warning', 'Error')]
45+
[String] $Level,
46+
47+
[Parameter(Position=3,
48+
Mandatory=$false)]
49+
[Boolean] $LogFile,
50+
51+
[Parameter(Position=4,
52+
Mandatory=$false)]
53+
[Boolean] $EventLog,
54+
55+
[Parameter(Position=5,
56+
Mandatory=$false)]
57+
[Boolean] $ConsoleOutput
58+
)
59+
60+
if ($Global:ScriptLogger -ne $null)
61+
{
62+
if ($PSBoundParameters.ContainsKey('Path'))
63+
{
64+
# Create an empty log file, if it does not exist
65+
if (-not (Test-Path -Path $Path))
66+
{
67+
New-Item -Path $Path -ItemType File | Out-Null
68+
}
69+
70+
# Only work with absolute path, makes error handling easier
71+
$Path = (Resolve-Path -Path $Path).Path
72+
73+
$Global:ScriptLogger.Path = $Path
74+
}
75+
76+
if ($PSBoundParameters.ContainsKey('Format'))
77+
{
78+
$Global:ScriptLogger.Format = $Format
79+
}
80+
81+
if ($PSBoundParameters.ContainsKey('Level'))
82+
{
83+
$Global:ScriptLogger.Level = $Level
84+
}
85+
86+
if ($PSBoundParameters.ContainsKey('LogFile'))
87+
{
88+
$Global:ScriptLogger.LogFile = $LogFile
89+
}
90+
91+
if ($PSBoundParameters.ContainsKey('EventLog'))
92+
{
93+
$Global:ScriptLogger.EventLog = $EventLog
94+
}
95+
96+
if ($PSBoundParameters.ContainsKey('ConsoleOutput'))
97+
{
98+
$Global:ScriptLogger.ConsoleOutput = $ConsoleOutput
99+
}
100+
}
101+
}

Functions/Start-ScriptLogger.ps1

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<#
2+
.SYNOPSIS
3+
4+
5+
.DESCRIPTION
6+
7+
8+
.PARAMETER Path
9+
The path to the log file.
10+
11+
.PARAMETER Format
12+
This parameter defines, how the log output will be formated. The format
13+
definition will be used to call the System.String.Format() method. The
14+
following values are used as arguments:
15+
{0} Timestamp as datetime value.
16+
{1} NetBIOS computer name.
17+
{2} Current session username.
18+
{3} Log entry level.
19+
{4} Message.
20+
21+
.PARAMETER Level
22+
The event log level. All log messages equal to or higher to the level will
23+
be logged. This is the level order:
24+
1. Verbose
25+
2. Information
26+
3. Warning
27+
4. Error
28+
29+
.PARAMETER SkipEventLog
30+
Skip the event log output. By default, all log messages will be written
31+
into the "Windows PowerShell" event log.
32+
33+
.PARAMETER HideConsoleOutput
34+
Hide the PowerShell console output. By default, all log messages are shown
35+
on the console.
36+
37+
.EXAMPLE
38+
C:\>
39+
40+
.EXAMPLE
41+
C:\>
42+
#>
43+
44+
function Start-ScriptLogger
45+
{
46+
[CmdletBinding()]
47+
param
48+
(
49+
[Parameter(Position=0,
50+
Mandatory=$false)]
51+
[ValidateScript({Test-Path -Path (Split-Path -Path $_ -Parent)})]
52+
[String] $Path = $Global:MyInvocation.MyCommand.Definition.Trim() + '.log',
53+
54+
[Parameter(Position=1,
55+
Mandatory=$false)]
56+
[ValidateScript({$_ -f (Get-Date), $Env:ComputerName, $Env:Username, 'Verbose', 'Message'})]
57+
[String] $Format = '{0:yyyy-MM-dd} {0:HH:mm:ss} {1} {2} {3,-11} {4}',
58+
59+
[Parameter(Position=2,
60+
Mandatory=$false)]
61+
[ValidateSet('Verbose', 'Information', 'Warning', 'Error')]
62+
[String] $Level = 'Verbose',
63+
64+
[Parameter(Position=3,
65+
Mandatory=$false)]
66+
[Switch] $NoLogFile,
67+
68+
[Parameter(Position=4,
69+
Mandatory=$false)]
70+
[Switch] $NoEventLog,
71+
72+
[Parameter(Position=5,
73+
Mandatory=$false)]
74+
[Switch] $NoConsoleOutput
75+
)
76+
77+
# Create an empty log file, if it does not exist
78+
if (-not (Test-Path -Path $Path))
79+
{
80+
New-Item -Path $Path -ItemType File | Out-Null
81+
}
82+
83+
# Only work with absolute path, makes error handling easier
84+
$Path = (Resolve-Path -Path $Path).Path
85+
86+
Write-Verbose "Enable script logger with log file '$Path'"
87+
88+
# Define global variable for the logging
89+
$Global:ScriptLogger = New-Object -TypeName PSObject -Property @{
90+
Enabled = $true
91+
Path = $Path
92+
Format = $Format
93+
Level = $Level
94+
LogFile = -not $NoLogFile.IsPresent
95+
EventLog = -not $NoEventLog.IsPresent
96+
ConsoleOutput = -not $NoConsoleOutput.IsPresent
97+
}
98+
$Global:ScriptLogger.PSTypeNames.Insert(0, 'ScriptLogger.Configuration')
99+
}

Functions/Stop-ScriptLogger.ps1

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<#
2+
.SYNOPSIS
3+
4+
5+
.DESCRIPTION
6+
7+
8+
.EXAMPLE
9+
C:\>
10+
11+
.EXAMPLE
12+
C:\>
13+
#>
14+
15+
function Stop-ScriptLogger
16+
{
17+
[CmdletBinding()]
18+
param
19+
(
20+
)
21+
22+
if ($Global:ScriptLogger -ne $null)
23+
{
24+
Remove-Variable -Scope Global -Name ScriptLogger -Force
25+
}
26+
}

Functions/Write-ErrorLog.ps1

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<#
2+
.SYNOPSIS
3+
4+
5+
.DESCRIPTION
6+
7+
8+
.PARAMETER Message
9+
10+
11+
.EXAMPLE
12+
C:\>
13+
14+
.EXAMPLE
15+
C:\>
16+
#>
17+
18+
function Write-ErrorLog
19+
{
20+
[CmdletBinding()]
21+
param
22+
(
23+
[Parameter(Mandatory=$true)]
24+
[String] $Message
25+
)
26+
27+
Write-Log -Message $Message -Level 'Error'
28+
}

Functions/Write-InformationLog.ps1

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<#
2+
.SYNOPSIS
3+
4+
5+
.DESCRIPTION
6+
7+
8+
.PARAMETER Message
9+
10+
11+
.EXAMPLE
12+
C:\>
13+
14+
.EXAMPLE
15+
C:\>
16+
#>
17+
18+
function Write-InformationLog
19+
{
20+
[CmdletBinding()]
21+
param
22+
(
23+
[Parameter(Mandatory=$true)]
24+
[String] $Message
25+
)
26+
27+
Write-Log -Message $Message -Level 'Information'
28+
}

0 commit comments

Comments
 (0)