Skip to content

Commit af57d43

Browse files
committed
Merge pull request #2 from claudiospizzi/dev
Release 1.1.0
2 parents 48315b0 + d6a96e1 commit af57d43

File tree

5 files changed

+124
-27
lines changed

5 files changed

+124
-27
lines changed

Functions/Start-ScriptLogger.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,7 @@ function Start-ScriptLogger
107107
ConsoleOutput = -not $NoConsoleOutput.IsPresent
108108
}
109109
$Global:ScriptLogger.PSTypeNames.Insert(0, 'ScriptLogger.Configuration')
110+
111+
# Return logger object
112+
return $Global:ScriptLogger
110113
}

Functions/Write-ErrorLog.ps1

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,26 @@
1616

1717
function Write-ErrorLog
1818
{
19-
[CmdletBinding()]
19+
[CmdletBinding(DefaultParameterSetName='Message')]
2020
param
2121
(
22-
[Parameter(Mandatory=$true)]
23-
[String] $Message
22+
[Parameter(Mandatory=$true,
23+
ParameterSetName='Message')]
24+
[String] $Message,
25+
26+
[Parameter(Mandatory=$true,
27+
ParameterSetName='ErrorRecord')]
28+
[System.Management.Automation.ErrorRecord] $ErrorRecord
2429
)
2530

26-
Write-Log -Message $Message -Level 'Error'
31+
switch ($PSCmdlet.ParameterSetName)
32+
{
33+
'Message' {
34+
Write-Log -Message $Message -Level 'Error'
35+
}
36+
37+
'ErrorRecord' {
38+
Write-Log -ErrorRecord $ErrorRecord
39+
}
40+
}
2741
}

Functions/Write-Log.ps1

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,24 @@
2020

2121
function Write-Log
2222
{
23-
[CmdletBinding()]
23+
[CmdletBinding(DefaultParameterSetName='Default')]
2424
param
2525
(
26-
[Parameter(Mandatory=$true)]
26+
[Parameter(Position=0,
27+
Mandatory=$true,
28+
ParameterSetName='Default')]
2729
[String] $Message,
2830

29-
[Parameter(Mandatory=$true)]
31+
[Parameter(Position=1,
32+
Mandatory=$true,
33+
ParameterSetName='Default')]
3034
[ValidateSet('Verbose', 'Information', 'Warning', 'Error')]
31-
[String] $Level
35+
[String] $Level,
36+
37+
[Parameter(Position=1,
38+
Mandatory=$true,
39+
ParameterSetName='ErrorRecord')]
40+
[System.Management.Automation.ErrorRecord] $ErrorRecord
3241
)
3342

3443
$ScriptLogger = Get-ScriptLogger
@@ -43,6 +52,17 @@ function Write-Log
4352
'Error' = 3
4453
}
4554

55+
# Check if the log level an error or an error record was submitted
56+
if ($PSCmdlet.ParameterSetName -eq 'Default' -and $Level -eq 'Error')
57+
{
58+
$ErrorRecord = New-Object -TypeName System.Management.Automation.ErrorRecord -ArgumentList $Message, 'Unknown', 'NotSpecified', $null
59+
}
60+
elseif ($PSCmdlet.ParameterSetName -eq 'ErrorRecord')
61+
{
62+
$Message = $ErrorRecord.ToString()
63+
$Level = 'Error'
64+
}
65+
4666
# Check the logging level
4767
if ($LevelMap[$Level] -ge $LevelMap[$ScriptLogger.Level])
4868
{
@@ -76,7 +96,7 @@ function Write-Log
7696
'Verbose' { Write-Verbose -Message $Message }
7797
'Information' { try { Write-Information -MessageData $Message } catch { Write-Host $Message } }
7898
'Warning' { Write-Warning -Message $Message }
79-
'Error' { Write-Error -Message $Message }
99+
'Error' { Write-Error -ErrorRecord $ErrorRecord }
80100
}
81101
}
82102
}

Tests/Start-ScriptLogger.Tests.ps1

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,11 @@ Describe 'Start-ScriptLogger' {
2525
$DefaultLogFile = $true
2626
$DefaultEventLog = $true
2727
$DefaultConsole = $true
28-
29-
## Convert default path from an relative to an absolute
30-
#if (-not (Test-Path -Path $DefaultPath))
31-
#{
32-
# New-Item -Path $DefaultPath -ItemType File | Out-Null
33-
#}
34-
#$DefaultPath = Resolve-Path -Path $DefaultPath | Select-Object -ExpandProperty Path
35-
#$DefaultPath | Remove-Item -Force
3628
}
3729

3830
It 'ParameterNone' {
3931

40-
Start-ScriptLogger
41-
42-
$ScriptLogger = Get-ScriptLogger
32+
$ScriptLogger = Start-ScriptLogger
4333

4434
$ScriptLogger | Should Not Be $null
4535

@@ -56,7 +46,7 @@ Describe 'Start-ScriptLogger' {
5646

5747
$ExpectedPath = 'TestDrive:\test.log'
5848

59-
Start-ScriptLogger -Path $ExpectedPath
49+
$ScriptLogger = Start-ScriptLogger -Path $ExpectedPath
6050

6151
$ScriptLogger | Should Not Be $null
6252

@@ -73,7 +63,7 @@ Describe 'Start-ScriptLogger' {
7363

7464
$ExpectedFormat = '{4} {3} {2} {1} {0}'
7565

76-
Start-ScriptLogger -Format $ExpectedFormat
66+
$ScriptLogger = Start-ScriptLogger -Format $ExpectedFormat
7767

7868
$ScriptLogger | Should Not Be $null
7969

@@ -90,7 +80,7 @@ Describe 'Start-ScriptLogger' {
9080

9181
$ExpectedLevel = 'Error'
9282

93-
Start-ScriptLogger -Level $ExpectedLevel
83+
$ScriptLogger = Start-ScriptLogger -Level $ExpectedLevel
9484

9585
$ScriptLogger | Should Not Be $null
9686

@@ -105,7 +95,7 @@ Describe 'Start-ScriptLogger' {
10595

10696
It 'ParameterNoLogFile' {
10797

108-
Start-ScriptLogger -NoLogFile
98+
$ScriptLogger = Start-ScriptLogger -NoLogFile
10999

110100
$ScriptLogger | Should Not Be $null
111101

@@ -120,7 +110,7 @@ Describe 'Start-ScriptLogger' {
120110

121111
It 'ParameterNoEventLog' {
122112

123-
Start-ScriptLogger -NoEventLog
113+
$ScriptLogger = Start-ScriptLogger -NoEventLog
124114

125115
$ScriptLogger | Should Not Be $null
126116

@@ -135,7 +125,7 @@ Describe 'Start-ScriptLogger' {
135125

136126
It 'ParameterNoConsoleOutput' {
137127

138-
Start-ScriptLogger -NoConsoleOutput
128+
$ScriptLogger = Start-ScriptLogger -NoConsoleOutput
139129

140130
$ScriptLogger | Should Not Be $null
141131

Tests/Write-ErrorLog.Tests.ps1

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,23 @@ InModuleScope ScriptLogger {
3030
}
3131
}
3232

33+
Context 'MockInnerCallErrorRecord' {
34+
35+
Mock Write-Log -ModuleName ScriptLogger -ParameterFilter { $ErrorRecord.CategoryInfo.Reason -eq 'RuntimeException' }
36+
37+
It 'InnerLevel' {
38+
39+
Write-ErrorLog -ErrorRecord $(try { 0 / 0 } catch { $_ })
40+
41+
Assert-MockCalled Write-Log -Times 1
42+
}
43+
}
44+
3345
Context 'Output' {
3446

3547
Mock Get-Date -ModuleName ScriptLogger { [DateTime] '2000-12-31 01:02:03' }
3648

37-
Mock Write-Error -ModuleName ScriptLogger -ParameterFilter { $Message -eq 'My Error' }
49+
Mock Write-Error -ModuleName ScriptLogger -ParameterFilter { $ErrorRecord.CategoryInfo.Reason -eq 'Exception' }
3850

3951
BeforeAll {
4052

@@ -87,5 +99,63 @@ InModuleScope ScriptLogger {
8799
Stop-ScriptLogger
88100
}
89101
}
102+
103+
Context 'OutputErrorRecord' {
104+
105+
Mock Get-Date -ModuleName ScriptLogger { [DateTime] '2000-12-31 01:02:03' }
106+
107+
Mock Write-Error -ModuleName ScriptLogger -ParameterFilter { $ErrorRecord.CategoryInfo.Reason -eq 'RuntimeException' }
108+
109+
BeforeAll {
110+
111+
$Path = 'TestDrive:\test.log'
112+
}
113+
114+
It 'LogFile' {
115+
116+
Start-ScriptLogger -Path $Path -NoEventLog -NoConsoleOutput
117+
118+
Write-ErrorLog -ErrorRecord $(try { 0 / 0 } catch { $_ })
119+
120+
$Content = Get-Content -Path $Path
121+
$Content | Should Be "2000-12-31 01:02:03 $Env:ComputerName $Env:Username Error Attempted to divide by zero."
122+
}
123+
124+
It 'EventLog' {
125+
126+
Start-ScriptLogger -Path $Path -NoLogFile -NoConsoleOutput
127+
128+
$Before = Get-Date
129+
130+
Write-ErrorLog -ErrorRecord $(try { 0 / 0 } catch { $_ })
131+
132+
$Event = Get-EventLog -LogName 'Windows PowerShell' -Source 'PowerShell' -InstanceId 0 -EntryType Error -After $Before -Newest 1
133+
134+
$Event | Should Not Be $null
135+
$Event.EventID | Should Be 0
136+
$Event.CategoryNumber | Should Be 0
137+
$Event.EntryType | Should Be 'Error'
138+
$Event.Message | Should Be "The description for Event ID '0' in Source 'PowerShell' cannot be found. The local computer may not have the necessary registry information or message DLL files to display the message, or you may not have permission to access them. The following information is part of the event:'Attempted to divide by zero.'"
139+
$Event.Source | Should Be 'PowerShell'
140+
$Event.InstanceId | Should Be 0
141+
}
142+
143+
It 'ConsoleOutput' {
144+
145+
Start-ScriptLogger -Path $Path -NoLogFile -NoEventLog
146+
147+
$Before = Get-Date
148+
149+
Write-ErrorLog -ErrorRecord $(try { 0 / 0 } catch { $_ })
150+
151+
Assert-MockCalled -CommandName 'Write-Error' -Times 1 -Exactly
152+
}
153+
154+
AfterEach {
155+
156+
Get-ScriptLogger | Remove-Item -Force
157+
Stop-ScriptLogger
158+
}
159+
}
90160
}
91161
}

0 commit comments

Comments
 (0)