Skip to content

Commit 45b1292

Browse files
committed
refactor(core): move root-scoped process/task cleanup into Atlas.TasksProcs
Stop-ProcessesUnderRoots and Stop-TasksUnderRoots lived in the general-purpose Atlas.Core alongside identically-named files in Atlas.TasksProcs, split by an unobvious rule, with a single consumer. Move them into Atlas.TasksProcs (renamed to the module's Stop-AtlasProcessUnderRoot / Stop-AtlasScheduledTaskUnderRoot convention, bodies verbatim), delete the Core copies, and point the sole consumer (Stop-AtlasFolderProcess.ps1) at the new home so each domain has one owner.
1 parent 5e4394f commit 45b1292

10 files changed

Lines changed: 129 additions & 110 deletions

File tree

playbook/Executables/AtlasModules/Scripts/Internal/Stop-AtlasFolderProcess.ps1

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ if (-not (Test-Path -LiteralPath $coreModule -PathType Leaf)) {
1010

1111
Import-Module $coreModule -Force
1212

13+
$tasksProcsModule = Join-Path -Path $executablesRoot -ChildPath 'AtlasModules\Scripts\Modules\Atlas.TasksProcs\Atlas.TasksProcs.psd1'
14+
if (-not (Test-Path -LiteralPath $tasksProcsModule -PathType Leaf)) {
15+
throw "Atlas tasks/processes module '$tasksProcsModule' is missing."
16+
}
17+
18+
Import-Module $tasksProcsModule -Force
19+
1320
$windir = [Environment]::GetFolderPath('Windows')
1421
$targetRoots = @(
1522
Join-Path -Path $windir -ChildPath 'AtlasModules'
@@ -28,8 +35,8 @@ if (-not $targetRoots) {
2835
}
2936

3037
$rootsLower = $targetRoots | ForEach-Object { ($_ + '\').ToLowerInvariant() }
31-
Stop-ProcessesUnderRoots -RootsLower $rootsLower
32-
Stop-TasksUnderRoots -RootsLower $rootsLower
38+
Stop-AtlasProcessUnderRoot -RootsLower $rootsLower
39+
Stop-AtlasScheduledTaskUnderRoot -RootsLower $rootsLower
3340

3441
$timerExePath = Join-Path -Path $windir -ChildPath 'AtlasModules\Tools\SetTimerResolution.exe'
3542
if (Test-Path -LiteralPath $timerExePath -PathType Leaf) {
@@ -38,7 +45,7 @@ if (Test-Path -LiteralPath $timerExePath -PathType Leaf) {
3845
$stream.Dispose()
3946
}
4047
catch {
41-
Stop-ProcessesUnderRoots -RootsLower $rootsLower
48+
Stop-AtlasProcessUnderRoot -RootsLower $rootsLower
4249
Start-Sleep -Milliseconds 500
4350
}
4451
}

playbook/Executables/AtlasModules/Scripts/Modules/Atlas.Core/Atlas.Core.psd1

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@
2626
'Write-Title'
2727
'Read-Pause'
2828
'Read-MessageBox'
29-
# Process/task helpers (absorbed from the former Utils module)
30-
'Stop-ProcessesUnderRoots'
31-
'Stop-TasksUnderRoots'
3229
)
3330
CmdletsToExport = @()
3431
VariablesToExport = @()

playbook/Executables/AtlasModules/Scripts/Modules/Atlas.Core/Atlas.Core.psm1

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ foreach ($domainModule in @(
66
'Logging.ps1'
77
'Privilege.ps1'
88
'Ui.ps1'
9-
'Processes.ps1'
10-
'ScheduledTasks.ps1'
119
'RunAsUser.ps1'
1210
)) {
1311
$domainPath = Join-Path -Path $domainRoot -ChildPath $domainModule
@@ -22,6 +20,5 @@ Export-ModuleMember -Function @(
2220
'Get-AtlasContext', 'Test-AtlasOption', 'New-AtlasFlag', 'Test-AtlasFlag',
2321
'Write-AtlasLog', 'Start-AtlasPhase', 'Stop-AtlasPhase',
2422
'Test-AtlasAdmin', 'Test-AtlasTrustedInstaller', 'Assert-AtlasPrivilege', 'Invoke-AtlasTrustedInstaller', 'Invoke-AtlasAsUser', 'Get-AtlasUserProcessCommandLine',
25-
'Write-Title', 'Read-Pause', 'Read-MessageBox',
26-
'Stop-ProcessesUnderRoots', 'Stop-TasksUnderRoots'
23+
'Write-Title', 'Read-Pause', 'Read-MessageBox'
2724
)

playbook/Executables/AtlasModules/Scripts/Modules/Atlas.Core/Domain/Processes.ps1

Lines changed: 0 additions & 30 deletions
This file was deleted.

playbook/Executables/AtlasModules/Scripts/Modules/Atlas.Core/Domain/ScheduledTasks.ps1

Lines changed: 0 additions & 69 deletions
This file was deleted.

playbook/Executables/AtlasModules/Scripts/Modules/Atlas.TasksProcs/Atlas.TasksProcs.psd1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
'Enable-AtlasScheduledTask'
1111
'Remove-AtlasScheduledTask'
1212
'Stop-AtlasProcess'
13+
'Stop-AtlasProcessUnderRoot'
14+
'Stop-AtlasScheduledTaskUnderRoot'
1315
)
1416
CmdletsToExport = @()
1517
VariablesToExport = @()

playbook/Executables/AtlasModules/Scripts/Modules/Atlas.TasksProcs/Atlas.TasksProcs.psm1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ foreach ($domainModule in @(
2323

2424
Export-ModuleMember -Function @(
2525
'Disable-AtlasScheduledTask', 'Enable-AtlasScheduledTask', 'Remove-AtlasScheduledTask',
26-
'Stop-AtlasProcess'
26+
'Stop-AtlasProcess', 'Stop-AtlasProcessUnderRoot', 'Stop-AtlasScheduledTaskUnderRoot'
2727
)

playbook/Executables/AtlasModules/Scripts/Modules/Atlas.TasksProcs/Domain/Processes.ps1

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,32 @@ function Stop-AtlasProcess {
2424
}
2525
}
2626
}
27+
28+
function Stop-AtlasProcessUnderRoot {
29+
param([string[]]$RootsLower)
30+
31+
foreach ($proc in Get-Process -ErrorAction SilentlyContinue) {
32+
if (-not $proc.Path) { continue }
33+
34+
$procPath = try {
35+
([System.IO.Path]::GetFullPath($proc.Path)).ToLowerInvariant()
36+
}
37+
catch {
38+
continue
39+
}
40+
41+
foreach ($root in $RootsLower) {
42+
if ($procPath.StartsWith($root)) {
43+
try {
44+
Stop-Process -Id $proc.Id -Force -ErrorAction Stop
45+
Wait-Process -Id $proc.Id -ErrorAction SilentlyContinue -Timeout 5
46+
}
47+
catch {
48+
continue
49+
}
50+
51+
break
52+
}
53+
}
54+
}
55+
}

playbook/Executables/AtlasModules/Scripts/Modules/Atlas.TasksProcs/Domain/ScheduledTasks.ps1

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,71 @@ function Remove-AtlasScheduledTask {
8686
Invoke-AtlasScheduledTaskCommand -Path $Path -OperationLabel 'delete' -IgnoreMissing:$IgnoreMissing `
8787
-Arguments @('/Delete', '/TN', $Path, '/F')
8888
}
89+
90+
function Stop-AtlasScheduledTaskUnderRoot {
91+
param([string[]]$RootsLower)
92+
93+
try {
94+
Import-Module ScheduledTasks -ErrorAction SilentlyContinue | Out-Null
95+
}
96+
catch {
97+
# Module may not be available on older systems; continue with fallbacks.
98+
$null = $_
99+
}
100+
101+
$tasks = @()
102+
try {
103+
$tasks = Get-ScheduledTask -ErrorAction Stop
104+
}
105+
catch {
106+
$tasks = @()
107+
}
108+
109+
foreach ($task in $tasks) {
110+
$matchesRoot = $false
111+
112+
foreach ($action in $task.Actions) {
113+
$execute = $null
114+
if ($action.PSObject.Properties.Match('Execute').Count) {
115+
$execute = $action.Execute
116+
}
117+
elseif ($action.PSObject.Properties.Match('Path').Count) {
118+
$execute = $action.Path
119+
}
120+
121+
if (-not $execute) { continue }
122+
123+
$executeLower = try {
124+
([System.IO.Path]::GetFullPath($execute)).ToLowerInvariant()
125+
}
126+
catch {
127+
$null
128+
}
129+
130+
if (-not $executeLower) { continue }
131+
132+
foreach ($root in $RootsLower) {
133+
if ($executeLower.StartsWith($root)) {
134+
$matchesRoot = $true
135+
break
136+
}
137+
}
138+
139+
if ($matchesRoot) { break }
140+
}
141+
142+
if (-not $matchesRoot) { continue }
143+
144+
try {
145+
Stop-ScheduledTask -TaskName $task.TaskName -TaskPath $task.TaskPath -ErrorAction SilentlyContinue
146+
}
147+
catch {
148+
# Ignore and fall back to schtasks below.
149+
$null = $_
150+
}
151+
}
152+
153+
foreach ($candidate in @('Force Timer Resolution', '\Force Timer Resolution')) {
154+
& schtasks.exe /End /TN $candidate 1>$null 2>$null
155+
}
156+
}

tests/Atlas.TasksProcs.Tests.ps1

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,21 @@ Describe 'Scheduled-task wrappers build the correct schtasks command' {
144144
}
145145
}
146146
}
147+
148+
# Root-scoped process/task cleanup helpers moved here from Atlas.Core (plan 019). These assert
149+
# the module now owns the renamed functions and that the old Core names are gone, not aliased.
150+
Describe 'Root-scoped cleanup helpers live in Atlas.TasksProcs' {
151+
It 'exports Stop-AtlasProcessUnderRoot and Stop-AtlasScheduledTaskUnderRoot' {
152+
$exported = (Get-Command -Module Atlas.TasksProcs).Name
153+
$exported | Should -Contain 'Stop-AtlasProcessUnderRoot'
154+
$exported | Should -Contain 'Stop-AtlasScheduledTaskUnderRoot'
155+
}
156+
157+
It 'no longer exposes the old Stop-ProcessesUnderRoots name' {
158+
Get-Command -Name 'Stop-ProcessesUnderRoots' -ErrorAction SilentlyContinue | Should -BeNullOrEmpty
159+
}
160+
161+
It 'no longer exposes the old Stop-TasksUnderRoots name' {
162+
Get-Command -Name 'Stop-TasksUnderRoots' -ErrorAction SilentlyContinue | Should -BeNullOrEmpty
163+
}
164+
}

0 commit comments

Comments
 (0)