fix: ensure CommandNotFoundException is not handled as task not found

This commit is contained in:
Jeffrey
2026-08-23 00:34:52 +02:00
parent b7f612f36e
commit 20d206cb9b
2 changed files with 72 additions and 4 deletions
@@ -54,12 +54,12 @@ function Disable-TelemetryScheduledTasks {
try { try {
$result = Invoke-NonBlocking -ScriptBlock { $result = Invoke-NonBlocking -ScriptBlock {
param($path, $name) param($path, $name)
Import-Module ScheduledTasks -ErrorAction SilentlyContinue
try { try {
Import-Module ScheduledTasks -ErrorAction Stop
$taskObj = Get-ScheduledTask -TaskPath $path -TaskName $name -ErrorAction Stop $taskObj = Get-ScheduledTask -TaskPath $path -TaskName $name -ErrorAction Stop
} }
catch { 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 = $true; Status = 'NotFound' }
} }
return @{ Success = $false; Status = 'Error'; Error = $_.Exception.Message } return @{ Success = $false; Status = 'Error'; Error = $_.Exception.Message }
@@ -128,12 +128,12 @@ function Enable-TelemetryScheduledTasks {
try { try {
$result = Invoke-NonBlocking -ScriptBlock { $result = Invoke-NonBlocking -ScriptBlock {
param($path, $name) param($path, $name)
Import-Module ScheduledTasks -ErrorAction SilentlyContinue
try { try {
Import-Module ScheduledTasks -ErrorAction Stop
$taskObj = Get-ScheduledTask -TaskPath $path -TaskName $name -ErrorAction Stop $taskObj = Get-ScheduledTask -TaskPath $path -TaskName $name -ErrorAction Stop
} }
catch { 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 = $true; Status = 'NotFound' }
} }
return @{ Success = $false; Status = 'Error'; Error = $_.Exception.Message } return @{ Success = $false; Status = 'Error'; Error = $_.Exception.Message }
+68
View File
@@ -111,6 +111,40 @@ Describe 'Disable-TelemetryScheduledTasks' {
$result.Error | Should -Match 'scheduler unavailable' $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' { It 'treats an absent scheduled task as not found' {
Mock Invoke-NonBlocking { Mock Invoke-NonBlocking {
param($ScriptBlock, $ArgumentList) param($ScriptBlock, $ArgumentList)
@@ -220,6 +254,40 @@ Describe 'Enable-TelemetryScheduledTasks' {
$result.Error | Should -Match 'scheduler unavailable' $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 @( It 'reports <Status> task results' -ForEach @(
@{ Status = 'Enabled'; Expected = 'Enabled Scheduled Task' } @{ Status = 'Enabled'; Expected = 'Enabled Scheduled Task' }
@{ Status = 'AlreadyEnabled'; Expected = 'already enabled' } @{ Status = 'AlreadyEnabled'; Expected = 'already enabled' }