Skip to content

Commit 8695e7f

Browse files
committed
fix: keep native-command stderr from aborting schtasks/reg.exe on WinPS 5.1
Under Windows PowerShell 5.1 with $ErrorActionPreference = 'Stop', a native command that writes to stderr (captured via 2>&1) raises a terminating error. reg.exe writes its success message to stderr and schtasks.exe writes to stderr for a missing task, so on the 5.1 runtime the payload actually ships to, importing a .reg file or disabling/removing an absent scheduled task threw instead of succeeding/warning. pwsh 7 doesn't behave this way, so pwsh-only CI never caught it. Run each native call under a local 'Continue' preference. Surfaced by the new Windows PowerShell 5.1 test leg.
1 parent 91e602d commit 8695e7f

3 files changed

Lines changed: 32 additions & 11 deletions

File tree

playbook/Executables/AtlasModules/Scripts/Modules/Atlas.Registry/Domain/RegFile.ps1

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,14 @@ function Import-AtlasRegFile {
1818
}
1919

2020
$regExePath = Join-Path -Path ([Environment]::GetFolderPath('System')) -ChildPath 'reg.exe'
21-
$output = & $regExePath import "$Path" 2>&1
21+
$previousErrorActionPreference = $ErrorActionPreference
22+
$ErrorActionPreference = 'Continue'
23+
try {
24+
$output = & $regExePath import "$Path" 2>&1
25+
}
26+
finally {
27+
$ErrorActionPreference = $previousErrorActionPreference
28+
}
2229
if ($LASTEXITCODE -ne 0) {
2330
$details = (@($output) | ForEach-Object { "$_" }) -join ' '
2431
throw "reg.exe import failed for '$Path' (exit code $LASTEXITCODE): $details"

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,14 @@ function Invoke-AtlasScheduledTaskCommand {
2121
[switch]$IgnoreMissing
2222
)
2323

24-
$output = & schtasks.exe @Arguments 2>&1
24+
$previousErrorActionPreference = $ErrorActionPreference
25+
$ErrorActionPreference = 'Continue'
26+
try {
27+
$output = & schtasks.exe @Arguments 2>&1
28+
}
29+
finally {
30+
$ErrorActionPreference = $previousErrorActionPreference
31+
}
2532
if ($LASTEXITCODE -eq 1) {
2633
if (-not $IgnoreMissing) {
2734
Write-AtlasLog -Level Warning -Message "Scheduled task '$Path' was not found; nothing to $OperationLabel."

playbook/Executables/AtlasModules/Scripts/Modules/Atlas.Tweaks/Domain/Invoke.ps1

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -123,16 +123,23 @@ function Invoke-AtlasTweakScheduledTaskEntries {
123123
# locale-independent existence probe decides which failure this actually is -
124124
# otherwise access-denied and similar real errors would be logged as a
125125
# harmless missing task.
126-
$null = & schtasks.exe /Query /TN "$taskPath" 2>&1
127-
if ($LASTEXITCODE -ne 0) {
128-
Write-AtlasLog -Message "Scheduled task '$taskPath' was not found; nothing to $($operation.ToLowerInvariant())." -Level Warning
129-
continue
130-
}
126+
$previousErrorActionPreference = $ErrorActionPreference
127+
$ErrorActionPreference = 'Continue'
128+
try {
129+
$null = & schtasks.exe /Query /TN "$taskPath" 2>&1
130+
if ($LASTEXITCODE -ne 0) {
131+
Write-AtlasLog -Message "Scheduled task '$taskPath' was not found; nothing to $($operation.ToLowerInvariant())." -Level Warning
132+
continue
133+
}
131134

132-
$output = & schtasks.exe /Change /TN "$taskPath" $stateArgument 2>&1
133-
if ($LASTEXITCODE -ne 0) {
134-
$details = (@($output) | ForEach-Object { "$_" }) -join ' '
135-
throw "schtasks.exe exited with code $LASTEXITCODE - $details"
135+
$output = & schtasks.exe /Change /TN "$taskPath" $stateArgument 2>&1
136+
if ($LASTEXITCODE -ne 0) {
137+
$details = (@($output) | ForEach-Object { "$_" }) -join ' '
138+
throw "schtasks.exe exited with code $LASTEXITCODE - $details"
139+
}
140+
}
141+
finally {
142+
$ErrorActionPreference = $previousErrorActionPreference
136143
}
137144
}
138145
catch {

0 commit comments

Comments
 (0)