Improve error reporting & handling (#741)

This commit is contained in:
Jeffrey
2026-08-24 20:34:50 +02:00
committed by GitHub
parent fff1fcd0b2
commit ef8811d28d
27 changed files with 1426 additions and 450 deletions
+131
View File
@@ -95,6 +95,69 @@ Describe 'Disable-TelemetryScheduledTasks' {
$result.Error | Should -Match 'access denied'
}
It 'returns an error when scheduled-task lookup fails' {
Mock Invoke-NonBlocking {
param($ScriptBlock, $ArgumentList)
$script:taskBlock = $ScriptBlock
$script:taskArguments = $ArgumentList
}
Mock Import-Module {}
Mock Get-ScheduledTask { throw 'scheduler unavailable' }
Disable-TelemetryScheduledTasks
$result = & $script:taskBlock @script:taskArguments
$result.Status | Should -Be 'Error'
$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)
$script:taskBlock = $ScriptBlock
$script:taskArguments = $ArgumentList
}
Mock Import-Module {}
Mock Get-ScheduledTask { $null }
Disable-TelemetryScheduledTasks
(& $script:taskBlock @script:taskArguments).Status | Should -Be 'NotFound'
}
It 'reports <Status> task results' -ForEach @(
@{ Status = 'Disabled'; Expected = 'Disabled Scheduled Task' }
@{ Status = 'AlreadyDisabled'; Expected = 'already disabled' }
@@ -108,6 +171,15 @@ Describe 'Disable-TelemetryScheduledTasks' {
Should -Invoke Write-Host -Times 1 -Exactly -ParameterFilter { $Object -like "*$Expected*" }
}
It 'returns false for an unknown scheduler result' {
Mock Get-TelemetryScheduledTasks { @(@{ Path = '\Microsoft\Windows\Test\'; Name = 'Telemetry' }) }
Mock Invoke-NonBlocking { $null }
Mock Write-Warning {}
Disable-TelemetryScheduledTasks | Should -BeFalse
Should -Invoke Write-Warning -Times 1 -Exactly
}
}
Describe 'Enable-TelemetryScheduledTasks' {
@@ -166,6 +238,56 @@ Describe 'Enable-TelemetryScheduledTasks' {
Should -Invoke Enable-ScheduledTask -Times 1 -Exactly -ParameterFilter { $TaskPath -eq '\Microsoft\Windows\Test\' -and $TaskName -eq 'Second' }
}
It 'returns an error when scheduled-task lookup fails' {
Mock Invoke-NonBlocking {
param($ScriptBlock, $ArgumentList)
$script:taskBlock = $ScriptBlock
$script:taskArguments = $ArgumentList
}
Mock Import-Module {}
Mock Get-ScheduledTask { throw 'scheduler unavailable' }
Enable-TelemetryScheduledTasks
$result = & $script:taskBlock @script:taskArguments
$result.Status | Should -Be 'Error'
$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 <Status> task results' -ForEach @(
@{ Status = 'Enabled'; Expected = 'Enabled Scheduled Task' }
@{ Status = 'AlreadyEnabled'; Expected = 'already enabled' }
@@ -179,4 +301,13 @@ Describe 'Enable-TelemetryScheduledTasks' {
Should -Invoke Write-Host -Times 1 -Exactly -ParameterFilter { $Object -like "*$Expected*" }
}
It 'returns false when the scheduler throws' {
Mock Get-TelemetryScheduledTasks { @(@{ Path = '\Microsoft\Windows\Test\'; Name = 'Telemetry' }) }
Mock Invoke-NonBlocking { throw 'scheduler unavailable' }
Mock Write-Warning {}
Enable-TelemetryScheduledTasks | Should -BeFalse
Should -Invoke Write-Warning -Times 1 -Exactly -ParameterFilter { $Message -match 'scheduler unavailable' }
}
}