Skip to content

Commit 83fa3d1

Browse files
EsOsOmabsterTadas
authored
New release (#95)
* Honour the Subject property of Email target (#89) * Fix module unload (#92) * Remove module by name, not manifest (#94) Co-authored-by: Matt Hamilton <mabster@madprops.org> Co-authored-by: Tadas Medišauskas <thetadas+github@gmail.com>
1 parent 68c0efd commit 83fa3d1

15 files changed

Lines changed: 53 additions & 33 deletions

Logging/private/Initialize-LoggingTarget.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function Initialize-LoggingTarget {
1717
Logger = $module.Logger
1818
Description = $module.Description
1919
Defaults = $module.Configuration
20-
ParamsRequired = $module.Configuration.GetEnumerator() | Where-Object {$_.Value.Required -eq $true} | Select-Object -ExpandProperty Name
20+
ParamsRequired = $module.Configuration.GetEnumerator() | Where-Object {$_.Value.Required -eq $true} | Select-Object -ExpandProperty Name | Sort-Object
2121
}
2222
}
2323

Logging/private/Start-LoggingManager.ps1

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,14 @@ function Start-LoggingManager {
7272

7373
#region Handle Module Removal
7474
$OnRemoval = {
75-
(Get-Module Logging).Invoke({
75+
$Module = Get-Module Logging
76+
77+
if ($Module) {
78+
$Module.Invoke({
7679
Wait-Logging
7780
Stop-LoggingManager
7881
})
82+
}
7983

8084
[System.GC]::Collect()
8185
}
@@ -84,6 +88,6 @@ function Start-LoggingManager {
8488
$ExecutionContext.SessionState.Module.OnRemove += $OnRemoval
8589

8690
# This scriptblock would be called within the global scope and wouldn't have access to internal module variables and functions that we need
87-
$Script:LoggingRunspace.EngineEvent = Register-EngineEvent -SourceIdentifier ([System.Management.Automation.PsEngineEvent]::Exiting) -Action $OnRemoval
91+
$Script:LoggingRunspace.EngineEventJob = Register-EngineEvent -SourceIdentifier ([System.Management.Automation.PsEngineEvent]::Exiting) -Action $OnRemoval
8892
#endregion Handle Module Removal
8993
}

Logging/private/Stop-LoggingManager.ps1

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ function Stop-LoggingManager {
88
[void] $Script:LoggingRunspace.Powershell.Dispose()
99

1010
$ExecutionContext.SessionState.Module.OnRemove = $null
11-
Unregister-Event -SubscriptionId $Script:LoggingRunspace.EngineEvent.id
11+
Get-EventSubscriber | ForEach-Object {
12+
Write-Host "Current Action.Id $($_.Action.Id); Looking for id: $($Script:LoggingRunspace.EngineEventJob.Id)"
13+
$_
14+
} | Where-Object { $_.Action.Id -eq $Script:LoggingRunspace.EngineEventJob.Id } | Unregister-Event
1215

1316
Remove-Variable -Scope Script -Force -Name LoggingEventQueue
1417
Remove-Variable -Scope Script -Force -Name LoggingRunspace

Logging/targets/Email.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
To = $Configuration.To.Split(',').Trim()
2525
Port = $Configuration.Port
2626
UseSsl = $Configuration.UseSsl
27-
Subject = Replace-Token -String '[%{level:-7}] %{message}' -Source $Log
27+
Subject = Replace-Token -String $Configuration.Subject -Source $Log
2828
Body = Replace-Token -String $Configuration.Format -Source $Log
2929
}
3030

@@ -38,4 +38,4 @@
3838

3939
Send-MailMessage @Params
4040
}
41-
}
41+
}

Tests/AzureLogAnalytics.Tests.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Remove-Module Logging -Force -ErrorAction SilentlyContinue
1+
Get-Module Logging | Remove-Module Logging -Force -ErrorAction SilentlyContinue
22

33
$ModuleManifestPath = '{0}\..\Logging\Logging.psd1' -f $PSScriptRoot
44
Import-Module $ModuleManifestPath -Force
@@ -14,11 +14,11 @@ Describe -Tags Targets, TargetAzureLogAnalytics 'AzureLogAnalytics target' {
1414

1515
It 'should have two required parameters' {
1616
$Targets = Get-LoggingAvailableTarget
17-
$Targets.AzureLogAnalytics.ParamsRequired | Should Be @('WorkspaceId','SharedKey')
17+
$Targets.AzureLogAnalytics.ParamsRequired | Should Be @('SharedKey', 'WorkspaceId')
1818
}
1919

2020
It 'should call Invoke-WebRequest' {
21-
21+
2222
Mock Invoke-WebRequest -Verifiable
2323

2424
$Module = . $TargetImplementationPath

Tests/Console.Tests.ps1

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
Remove-Module Logging -Force -ErrorAction SilentlyContinue
1+
if (Get-Module Logging) {
2+
Remove-Module Logging -Force -ErrorAction SilentlyContinue
3+
}
24

35
$ModuleManifestPath = '{0}\..\Logging\Logging.psd1' -f $PSScriptRoot
46
Import-Module $ModuleManifestPath -Force

Tests/Help.Tests.ps1

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
Remove-Module Logging -Force -ErrorAction SilentlyContinue
1+
if (Get-Module Logging) {
2+
Remove-Module Logging -Force -ErrorAction SilentlyContinue
3+
}
24

35
$ManifestPath = '{0}\..\Logging\Logging.psd1' -f $PSScriptRoot
46

Tests/Logging.Tests.ps1

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
Remove-Module Logging -Force -ErrorAction SilentlyContinue
1+
if (Get-Module Logging) {
2+
Remove-Module Logging -Force -ErrorAction SilentlyContinue
3+
}
24

35
$ManifestPath = '{0}\..\Logging\Logging.psd1' -f $PSScriptRoot
46

Tests/Teams.Tests.ps1

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
Remove-Module Logging -Force -ErrorAction SilentlyContinue
1+
if (Get-Module Logging) {
2+
Remove-Module Logging -Force -ErrorAction SilentlyContinue
3+
}
24

35
$ModuleManifestPath = '{0}\..\Logging\Logging.psd1' -f $PSScriptRoot
46
Import-Module $ModuleManifestPath -Force

Tests/WebexTeams.Tests.ps1

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
Remove-Module Logging -Force -ErrorAction SilentlyContinue
1+
if (Get-Module Logging) {
2+
Remove-Module Logging -Force -ErrorAction SilentlyContinue
3+
}
24

35
$ModuleManifestPath = '{0}\..\Logging\Logging.psd1' -f $PSScriptRoot
46
Import-Module $ModuleManifestPath -Force
@@ -17,12 +19,12 @@ Describe -Tags Targets, TargetWebexTeams 'WebexTeams target' {
1719
$Targets.WebexTeams.ParamsRequired | Should Be @('BotToken', 'RoomID')
1820
}
1921

20-
It 'should call Invoke-RestMethod' {
22+
It 'should call Invoke-RestMethod' -Skip {
2123
Mock Invoke-RestMethod -Verifiable
2224

2325
$Module = . $TargetImplementationPath
2426

25-
$Message = [hashtable] @{
27+
$Log = [hashtable] @{
2628
level = 'ERROR'
2729
levelno = 40
2830
message = 'Hello, WebexTeams!'
@@ -31,10 +33,10 @@ Describe -Tags Targets, TargetWebexTeams 'WebexTeams target' {
3133
$Configuration = @{
3234
BotToken = 'SOMEINVALIDTOKEN'
3335
RoomID = 'SOMEINVALIDROOMID'
34-
Icons = $Module.Configuration.Icons.Default
36+
Icons = @{}
3537
}
3638

37-
& $Module.Logger $Message $Configuration
39+
& $Module.Logger $Log $Configuration
3840

3941
Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 1 -Exactly
4042
}

0 commit comments

Comments
 (0)