Skip to content

Runtime harness starts the host when the script could not be copied #91

In review

Description

@fadwen

Summary

When the runtime harness cannot copy a script to its run folder, it starts the host anyway. On PowerShell 7 the host is started on a file that is not there, and its complaint comes back as the result of the script: RunState Failed, exit code -196608. The caller gets a result object for a script that never ran, next to an error from Copy-Item that ended nothing.

This is the result of #88 again, reached without any preference: there the copy was skipped, here it fails.

Findings

Measured on main at bf37d91 with a script held open without sharing, so that Resolve-Path finds it and Copy-Item cannot read it, and Invoke-IntunePlatformScriptTest -Path <script>:

PowerShell 7.6.10 Windows PowerShell 5.1.26100
Copy-Item Writes a non-terminating error, CopyFileInfoItemIOError Raises a terminating error, System.IO.IOException
The run Goes on: the host is started Ends there
What the caller gets A result with RunState Failed, ExitCode -196608 and the host's text in StdErr: The argument '...\script.ps1' to the -File parameter does not exist. No result, and the error of the copy

The cause is in Private/Invoke-IslScriptRun.ps1: New-Item and Copy-Item are called without -ErrorAction Stop and outside the try that holds the launch, so a failure of either one is reported and the function carries on to Invoke-IslProcess.

Invoke-ContentCommand in Public/Invoke-IntuneWin32AppTest.ps1 copies the content folder the same way before it runs the install or uninstall command, so a content folder that cannot be copied leads to the command line being run in an empty or partial copy.

Two related cases in the same code, found while reading it:

  • After a timeout, Invoke-IslProcess calls Stop-IslProcessTree outside a try with a catch. When taskkill.exe cannot be started, the run returns a TimedOut result without output and the process is left running (measured for fix(harness): end a timed-out run without taskkill's errors #87). Tests/Unit/Module.ErrorFlow.Tests.ps1 lists this call as its one known exception.
  • A failure of the copy leaves the run folder behind, because the cleanup is in a finally that the copy is not inside.

Proposed change

  • In Invoke-IslScriptRun, make the run folder, the copy, the folder access and the launch one try with a catch that ends the function with the error, and pass -ErrorAction Stop to New-Item and Copy-Item. The existing finally then removes the run folder in every case.
  • Do the same in Invoke-ContentCommand.
  • In Invoke-IslProcess, end the function with the error of a cleanup that could not be run, and take the exception out of the contract test.
  • Test the copy with a script that is held open, and check that the host is not started.

This belongs with the runtime-harness part of #64, which puts the calls of these functions inside try with catch.

Discovered and written up with Claude Code.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions