diff --git a/Scripts/Features/Telemetry-ScheduledTasks.ps1 b/Scripts/Features/Telemetry-ScheduledTasks.ps1 index f196ef2..36af8f1 100644 --- a/Scripts/Features/Telemetry-ScheduledTasks.ps1 +++ b/Scripts/Features/Telemetry-ScheduledTasks.ps1 @@ -54,12 +54,12 @@ function Disable-TelemetryScheduledTasks { try { $result = Invoke-NonBlocking -ScriptBlock { param($path, $name) - Import-Module ScheduledTasks -ErrorAction SilentlyContinue try { + Import-Module ScheduledTasks -ErrorAction Stop $taskObj = Get-ScheduledTask -TaskPath $path -TaskName $name -ErrorAction Stop } catch { - if ($_.CategoryInfo.Category -eq [System.Management.Automation.ErrorCategory]::ObjectNotFound) { + if ($_.Exception -isnot [System.Management.Automation.CommandNotFoundException] -and $_.CategoryInfo.Category -eq [System.Management.Automation.ErrorCategory]::ObjectNotFound) { return @{ Success = $true; Status = 'NotFound' } } return @{ Success = $false; Status = 'Error'; Error = $_.Exception.Message } @@ -128,12 +128,12 @@ function Enable-TelemetryScheduledTasks { try { $result = Invoke-NonBlocking -ScriptBlock { param($path, $name) - Import-Module ScheduledTasks -ErrorAction SilentlyContinue try { + Import-Module ScheduledTasks -ErrorAction Stop $taskObj = Get-ScheduledTask -TaskPath $path -TaskName $name -ErrorAction Stop } catch { - if ($_.CategoryInfo.Category -eq [System.Management.Automation.ErrorCategory]::ObjectNotFound) { + if ($_.Exception -isnot [System.Management.Automation.CommandNotFoundException] -and $_.CategoryInfo.Category -eq [System.Management.Automation.ErrorCategory]::ObjectNotFound) { return @{ Success = $true; Status = 'NotFound' } } return @{ Success = $false; Status = 'Error'; Error = $_.Exception.Message } diff --git a/Tests/Telemetry-ScheduledTasks.Tests.ps1 b/Tests/Telemetry-ScheduledTasks.Tests.ps1 index 1b5a522..2375fad 100644 --- a/Tests/Telemetry-ScheduledTasks.Tests.ps1 +++ b/Tests/Telemetry-ScheduledTasks.Tests.ps1 @@ -111,6 +111,40 @@ Describe 'Disable-TelemetryScheduledTasks' { $result.Error | Should -Match 'scheduler unavailable' } + It 'returns an error when the scheduled-task module cannot load' { + Mock Invoke-NonBlocking { + param($ScriptBlock, $ArgumentList) + $script:taskBlock = $ScriptBlock + $script:taskArguments = $ArgumentList + } + Mock Import-Module { throw 'module unavailable' } + + Disable-TelemetryScheduledTasks + $result = & $script:taskBlock @script:taskArguments + + $result.Status | Should -Be 'Error' + $result.Error | Should -Match 'module unavailable' + } + + It 'returns an error when the scheduled-task command is unavailable' { + Mock Invoke-NonBlocking { + param($ScriptBlock, $ArgumentList) + $script:taskBlock = $ScriptBlock + $script:taskArguments = $ArgumentList + } + Mock Import-Module {} + Mock Get-ScheduledTask { + throw [System.Management.Automation.ErrorRecord]::new( + [System.Management.Automation.CommandNotFoundException]::new('Get-ScheduledTask unavailable'), + 'CommandNotFoundException', + [System.Management.Automation.ErrorCategory]::ObjectNotFound, + $null) + } + + Disable-TelemetryScheduledTasks + (& $script:taskBlock @script:taskArguments).Status | Should -Be 'Error' + } + It 'treats an absent scheduled task as not found' { Mock Invoke-NonBlocking { param($ScriptBlock, $ArgumentList) @@ -220,6 +254,40 @@ Describe 'Enable-TelemetryScheduledTasks' { $result.Error | Should -Match 'scheduler unavailable' } + It 'returns an error when the scheduled-task module cannot load' { + Mock Invoke-NonBlocking { + param($ScriptBlock, $ArgumentList) + $script:taskBlock = $ScriptBlock + $script:taskArguments = $ArgumentList + } + Mock Import-Module { throw 'module unavailable' } + + Enable-TelemetryScheduledTasks + $result = & $script:taskBlock @script:taskArguments + + $result.Status | Should -Be 'Error' + $result.Error | Should -Match 'module unavailable' + } + + It 'returns an error when the scheduled-task command is unavailable' { + Mock Invoke-NonBlocking { + param($ScriptBlock, $ArgumentList) + $script:taskBlock = $ScriptBlock + $script:taskArguments = $ArgumentList + } + Mock Import-Module {} + Mock Get-ScheduledTask { + throw [System.Management.Automation.ErrorRecord]::new( + [System.Management.Automation.CommandNotFoundException]::new('Get-ScheduledTask unavailable'), + 'CommandNotFoundException', + [System.Management.Automation.ErrorCategory]::ObjectNotFound, + $null) + } + + Enable-TelemetryScheduledTasks + (& $script:taskBlock @script:taskArguments).Status | Should -Be 'Error' + } + It 'reports task results' -ForEach @( @{ Status = 'Enabled'; Expected = 'Enabled Scheduled Task' } @{ Status = 'AlreadyEnabled'; Expected = 'already enabled' }