mirror of
https://github.com/Raphire/Win11Debloat.git
synced 2026-08-23 16:12:08 +00:00
fix: ensure CommandNotFoundException is not handled as task not found
This commit is contained in:
@@ -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 }
|
||||
|
||||
@@ -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 <Status> task results' -ForEach @(
|
||||
@{ Status = 'Enabled'; Expected = 'Enabled Scheduled Task' }
|
||||
@{ Status = 'AlreadyEnabled'; Expected = 'already enabled' }
|
||||
|
||||
Reference in New Issue
Block a user