Repository navigation
Conversation
The three tests of the SYSTEM-context integration suite and the file-version test of Test-IntuneWin32Rule were skipped on every run, in an elevated session and on a computer that has notepad.exe as well. Their -Skip condition read a script-scoped variable that the file set in BeforeAll. Pester evaluates -Skip during discovery, before any BeforeAll runs, so the variable was unset, the condition was true, and a skipped test still leaves the run Passed. Both variables are now set in BeforeDiscovery, as the other suites with a skip condition do. The SYSTEM-context tests are the only ones that launch a script through a scheduled task as NT AUTHORITY\SYSTEM, so that path is tested again wherever the session is elevated. Refs #83
On Windows PowerShell 5.1, a run that timed out could end with a
terminating error instead of a result whose run state is TimedOut.
Invoke-IslProcess ended the process tree with
& taskkill.exe /PID <id> /T /F 2>&1 | Out-Null
and 5.1 turns a line that a native command writes to a redirected
error stream into an error record. With $ErrorActionPreference = 'Stop'
in a scope the function sees, as in a CI step or a script started with
-File, that record is terminating. taskkill writes such a line for a
process that is already gone, which is common in the scheduled-task
path: stopping the task has ended some of the processes, and ending
one ends its children.
The new private Stop-IslProcessTree starts taskkill.exe as a process
whose streams .NET redirects, so nothing it writes reaches PowerShell
as an error on either version. A process that is already gone is the
state the cleanup wants and is not reported. The exit code is read
now: when taskkill fails for another reason and the process is still
running, a warning says so.
Refs #86
# Conflicts: # CHANGELOG.md
fadwen
left a comment
There was a problem hiding this comment.
Verdict: approve after one should-fix.
Verified
- Closes #86, by reading the code. taskkill now runs as a .NET
Processwith both streams redirected and read asynchronously, so nothing it writes becomes an error record on Windows PowerShell 5.1. - Exit codes: 0 and 128 are treated as success. Any other code warns only if the process is still alive (ERR-23).
- Call sites: both places in
Invoke-IslProcessnow call the new function. - taskkill path: taken from
[Environment]::SystemDirectory, not from PATH.
Unverified (needs Windows): the 5.1 reproduction, the 5-round lab run as SYSTEM, and the CI runs 45148793084, 45148797974, 45148802814 and 45148807467. This PR has no checks because its base isn't main.
Should-fix: Private/Invoke-IslProcess.ps1 lines 101 and 243, the cleanup can be skipped by $WhatIfPreference and then hang
- Both calls pass
-Confirm:$falsebut not-WhatIf:$false. A module function sees a global$WhatIfPreference. - Reproduced: with
$global:WhatIfPreference = $true,Stop-IslProcessTree -ProcessId 999999 -Confirm:$falseprints "What if: Performing the operation "End"…" and runs no taskkill. - In the direct path, the next line is
$process.WaitForExit()with no timeout. A timed-out script that never exits therefore hangs the run for good, where the old& taskkillalways ran. - Fix: add
-WhatIf:$falseto both calls, the same way MOD-14 keeps non-publish steps out of-WhatIf. Add a test that sets$WhatIfPreference = $truearound a timed-outInvoke-IslProcessand checks that it returnsTimedOut.
Nits
- Should cleanup be ShouldProcess-gated at all? The cleanup is not a user-facing change, and both callers opt out of it. Dropping
SupportsShouldProcesswould remove this whole class of problem. If FUN-30 is read as covering it, keep it as it is plus the fix above. catch { $PSCmdlet.ThrowTerminatingError($_) }: if taskkill.exe can't be started, the timed-out run ends with that error instead of itsTimedOutresult. Writing a warning there would keep the run's result, which is what this PR is about.
Two comments in quality-gates.yml said that the hosted runner's token is not elevated and that the SYSTEM-context tests therefore skip in CI. They never ran because their skip condition was evaluated before its variable was set, not because of the runner: with the condition decided during discovery, the tests job and the Windows PowerShell 5.1 job each run the three tests, and the file takes 14 seconds where it took 62 milliseconds. The comments now say that the runner is elevated and that those jobs register and remove IntuneScriptLab-* scheduled tasks as SYSTEM on it. Refs #83
Stop-IslProcessTree declares SupportsShouldProcess, and the two calls in Invoke-IslProcess passed -Confirm:$false only. A function of the module sees a global $WhatIfPreference, so with it set the cleanup printed what it would do and ended nothing. In the direct path the next statement waits for the process without a time limit: a script that never exits would have held the run for good, where the taskkill call this function replaced always ran. Both calls pass -WhatIf:$false as well. The cleanup after a timeout is not the caller's to preview. The new test sets the preference around a timed-out run of a 30-second process: it took 29 seconds before this change and ends within the timeout now. Refs #86
|
The should-fix is addressed in 5664fdc. The two nits are answered below; neither is taken. Cleanup skipped under
Should the cleanup be gated by
The checks that could not be made in the sandbox
|
fadwen
left a comment
There was a problem hiding this comment.
Re-review after 5664fdc and the merge of main. Verdict: approve. The should-fix is resolved, and I accept your reasons for keeping both nits as they are.
Verified
- Diff against
main: only this PR's change is left (Stop-IslProcessTree, the two call sites, the tests and the CHANGELOG). - Both call sites: each passes
-Confirm:$false -WhatIf:$false, with a comment explaining why. - The new test: it sets
$WhatIfPreference = $truearound a timed-outInvoke-IslProcessand assertsTimedOutwithin 15 s. Its target is a process that ends by itself, so a regression fails the test instead of hanging the suite. - CI: run 45154229071 on this PR passed all five jobs, including
Windows PowerShell 5.1. - The #84 follow-up merged in here (35fd3e7):
Get-IslPropertyValuenow reads a genericDictionary[string,object]. The case I reproduced returns 1, andGet-IslPropertyValue.Tests.ps1passes 16/16 in my sandbox.
Optional follow-up (not blocking): as you say, the scheduled-task path under a WhatIf preference wasn't checked. Register-ScheduledTask and Start-ScheduledTask honour $WhatIfPreference themselves, so under it no task would be registered, and the run would only wait out its timeout. It's worth a look, or a -WhatIf:$false on those calls, in a later change.
test: decide the skip conditions of four tests during discovery
Summary
On Windows PowerShell 5.1, a run that timed out could end with a terminating error instead of a result whose run state is
TimedOut.Invoke-IslProcessended the process tree with& taskkill.exe /PID <id> /T /F 2>&1 | Out-Null. Windows PowerShell 5.1 turns a line that a native command writes to a redirected error stream into an error record, and with$ErrorActionPreference = 'Stop'in a scope the function sees, as in a CI step or a script started with-File, that record ends the command. taskkill writes such a line for a process that is already gone, which is common in the scheduled-task path. Closes #86.This pull request is stacked on #84 (
fix/strict-mode-root-module) and merges after it. #85 is now stacked on this one, because #85 is the change that lets the SYSTEM-context tests run in CI, and its timeout test is the one that met this defect.Changes
Stop-IslProcessTree. It startstaskkill.exeas a process whose streams .NET redirects, so nothing taskkill writes reaches PowerShell as an error, on either version. A process that is already gone (exit code 128) is the state the cleanup wants and is not reported. When taskkill fails for another reason and the process is still running, a warning says so; the exit code was never read before (ERR-23).Invoke-IslProcesscalls it in both places that ended a tree: after a direct run times out, and for each process of a scheduled-task run that timed out.Tests/Unit/Private/Stop-IslProcessTree.Tests.ps1runs it against a real tree,cmd.exerunningping.exe: the tree ends,-WhatIfends nothing, a process that is already gone neither fails nor warns under an error preference ofStop, a timed-outInvoke-IslProcessreturns its result under that preference, and it ends within its timeout when the caller's$WhatIfPreferenceis on.CHANGELOG.mdhas the entry under Fixed.Verification
-Fileon Windows 11: with$ErrorActionPreference = 'Stop',& taskkill.exe /PID 999999 /T /F 2>&1 | Out-Nullends with a terminatingRemoteException: ERROR: The process "999999" not found.on Windows PowerShell 5.1.26100 and with no error and exit code 128 on PowerShell 7.6.10. With the preferenceContinueneither version raises an error.Stop-IslProcessTree.Tests.ps1,Invoke-IslProcess.Tests.ps1andModule.Contract.Tests.ps1on PowerShell 7.6.10 and on Windows PowerShell 5.1: 47 passed, 0 failed on each, and noping.exewas left running. After the change for the WhatIf preference, the two process test files on both versions: 27 passed, 0 failed; the new test took 29 seconds and failed without that change.Invoke-ScriptAnalyzerat Error and Warning on the three changed script files: no findings.Test-StandardsCompliance.ps1of ai-powershell-standards 2.0.0 on the two new files: no findings../Tests/Invoke-Tests.ps1: on PowerShell 7.6.10 with-CodeCoverage1,069 passed, 3 failed, 7 skipped, coverage 95.31 percent; on Windows PowerShell 5.1 1,033 passed, 3 failed, 43 skipped. The three failures are the ones that fail on that computer without this change and pass in CI (Repair-IntuneScript corrupts an ANSI script on a host with a UTF-8 system code page #75 twice, In-box module table does not list UOPProviders, present on Windows 11 26H2 #76).NT AUTHORITY\SYSTEMunder Windows PowerShell 5.1.26100.9818 with$ErrorActionPreference = 'Stop'in the global scope, the SYSTEM-context suite five times over. Without this change: 10 passed, 5 failed, the timeout test every time, withERROR: The process "1584" not found.in two rounds andERROR: The process with PID 6640 (child process of PID 8524) could not be terminated.in three. With it: 15 passed, 0 failed, and noIntuneScriptLab-*scheduled task left behind. The unit tests of the two process functions in the same session: 26 passed.quality-gates.yml, dispatched on this branch, run 45148793084: all five jobs passed. The SYSTEM-context tests do not run on this branch; they do on test: decide the skip conditions of four tests during discovery #85, where the Windows PowerShell 5.1 job failed on the timeout test in run 45145190738 without this change and the workflow passed three times out of three with it (runs 45148797974, 45148802814 and 45148807467).Notes
Stop-IslProcessTreedeclaresSupportsShouldProcess(FUN-30). The two callers pass-Confirm:$falseand-WhatIf:$false, because the cleanup must neither wait for an answer nor be skipped by a preference: a function of the module sees a global$WhatIfPreference, and in the direct path the run waits for the process after the cleanup.