Repository navigation
fix(harness)!: raise run failures as terminating errors with IDs #92
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| function Invoke-IslNativeCommand { | ||
| <# | ||
| .SYNOPSIS | ||
| Runs a Windows executable to its end and returns its exit code and what it wrote. | ||
|
|
||
| .DESCRIPTION | ||
| For the small system tools the module calls for a side effect, such as taskkill.exe and | ||
| icacls.exe, whose error text is needed when they fail. The executable is started as a process | ||
| whose streams .NET redirects, not with the call operator: Windows PowerShell 5.1 turns a line | ||
| that a native command writes to a redirected error stream into an error record, and with an | ||
| error preference of Stop in the caller's scope the first such line ends the caller. | ||
|
|
||
| The exit code is returned, not judged. What a code means differs from tool to tool, so the | ||
| caller decides. | ||
|
|
||
| .PARAMETER FilePath | ||
| The full path of the executable. | ||
|
|
||
| .PARAMETER Arguments | ||
| The arguments, as one string in the form the executable parses. An argument that can hold a | ||
| space is quoted by the caller. | ||
|
|
||
| .EXAMPLE | ||
| $taskkill = Join-Path -Path ([System.Environment]::SystemDirectory) -ChildPath 'taskkill.exe' | ||
| $result = Invoke-IslNativeCommand -FilePath $taskkill -Arguments '/PID 4711 /T /F' | ||
|
|
||
| Ends process 4711 and what it started. $result.ExitCode is 128 when there is no such process. | ||
|
|
||
| .INPUTS | ||
| None. | ||
|
|
||
| .OUTPUTS | ||
| IntuneScriptLab.NativeCommandResult. ExitCode, StdOut and StdErr. | ||
| #> | ||
| [CmdletBinding()] | ||
| [OutputType('IntuneScriptLab.NativeCommandResult')] | ||
| param( | ||
| [Parameter(Mandatory)] | ||
| [string]$FilePath, | ||
|
|
||
| [Parameter()] | ||
| [string]$Arguments = '' | ||
| ) | ||
|
|
||
| $startInfo = [System.Diagnostics.ProcessStartInfo]::new() | ||
| $startInfo.FileName = $FilePath | ||
| $startInfo.Arguments = $Arguments | ||
| $startInfo.UseShellExecute = $false | ||
| $startInfo.CreateNoWindow = $true | ||
| $startInfo.RedirectStandardOutput = $true | ||
| $startInfo.RedirectStandardError = $true | ||
| $process = [System.Diagnostics.Process]::new() | ||
| $process.StartInfo = $startInfo | ||
| try { | ||
| $null = $process.Start() | ||
| # Both streams are read while the process runs, so that neither fills and blocks it | ||
| $outputTask = $process.StandardOutput.ReadToEndAsync() | ||
| $errorTask = $process.StandardError.ReadToEndAsync() | ||
| $process.WaitForExit() | ||
| $exitCode = $process.ExitCode | ||
| $outputText = "$($outputTask.GetAwaiter().GetResult())".Trim() | ||
| $errorText = "$($errorTask.GetAwaiter().GetResult())".Trim() | ||
| } catch { | ||
| $PSCmdlet.ThrowTerminatingError($_) | ||
| } finally { | ||
| $process.Dispose() | ||
| } | ||
|
|
||
| [pscustomobject]@{ | ||
| PSTypeName = 'IntuneScriptLab.NativeCommandResult' | ||
| ExitCode = $exitCode | ||
| StdOut = $outputText | ||
| StdErr = $errorText | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -92,23 +92,43 @@ | |
|
|
||
| $process = [System.Diagnostics.Process]::new() | ||
| $process.StartInfo = $startInfo | ||
| $null = $process.Start() | ||
| $process.StandardInput.Close() | ||
| $stdoutTask = $process.StandardOutput.ReadToEndAsync() | ||
| $stderrTask = $process.StandardError.ReadToEndAsync() | ||
| if (-not $process.WaitForExit($TimeoutSeconds * 1000)) { | ||
| $timedOut = $true | ||
| # The cleanup is not the caller's to preview or confirm. A function of this module | ||
| # sees a global $WhatIfPreference, and under it the tree would stay and the wait | ||
| # below would not end | ||
| Stop-IslProcessTree -ProcessId $process.Id -Confirm:$false -WhatIf:$false | ||
| $process.WaitForExit() | ||
| try { | ||
| $null = $process.Start() | ||
| } catch { | ||
| $process.Dispose() | ||
| $recordSplat = @{ | ||
| Message = "Could not start '$FilePath': $($_.Exception.GetBaseException().Message)" | ||
| ErrorId = 'ProcessStartFailed' | ||
| Category = [System.Management.Automation.ErrorCategory]::ResourceUnavailable | ||
| TargetObject = $FilePath | ||
| InnerException = $_.Exception | ||
| } | ||
| $PSCmdlet.ThrowTerminatingError((New-IslErrorRecord @recordSplat)) | ||
| } | ||
|
|
||
| try { | ||
| $process.StandardInput.Close() | ||
| $stdoutTask = $process.StandardOutput.ReadToEndAsync() | ||
| $stderrTask = $process.StandardError.ReadToEndAsync() | ||
| if (-not $process.WaitForExit($TimeoutSeconds * 1000)) { | ||
| $timedOut = $true | ||
| # The cleanup is not the caller's to preview or confirm. A function of this module | ||
| # sees a global $WhatIfPreference, and under it the tree would stay and the wait | ||
| # below would not end. A cleanup that could not be run ends the function with its | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A decision more than a conversion. Before, a |
||
| # error: the wait would not end either, and a TimedOut result would say that the | ||
| # tree is gone | ||
| Stop-IslProcessTree -ProcessId $process.Id -Confirm:$false -WhatIf:$false | ||
| $process.WaitForExit() | ||
| } | ||
| $stopwatch.Stop() | ||
| $stdout = $stdoutTask.GetAwaiter().GetResult() | ||
| $stderr = $stderrTask.GetAwaiter().GetResult() | ||
| $exitCode = $process.ExitCode | ||
| } catch { | ||
| $PSCmdlet.ThrowTerminatingError($_) | ||
| } finally { | ||
| $process.Dispose() | ||
| } | ||
| $stopwatch.Stop() | ||
| $stdout = $stdoutTask.GetAwaiter().GetResult() | ||
| $stderr = $stderrTask.GetAwaiter().GetResult() | ||
| $exitCode = $process.ExitCode | ||
| $process.Dispose() | ||
| } | ||
| else { | ||
| $runId = Split-Path -Path $WorkFolder -Leaf | ||
|
|
@@ -182,7 +202,15 @@ | |
| $null = Register-ScheduledTask @registerTaskSplat | ||
| } | ||
| catch { | ||
| throw "${refusal}: $($_.Exception.Message)" | ||
| $recordSplat = @{ | ||
| Message = "${refusal}: $($_.Exception.Message)" | ||
| ErrorId = 'ScheduledTaskNotRegistered' | ||
| Category = [System.Management.Automation.ErrorCategory]::PermissionDenied | ||
| TargetObject = $userName | ||
| ExceptionType = [System.UnauthorizedAccessException] | ||
| InnerException = $_.Exception | ||
| } | ||
| $PSCmdlet.ThrowTerminatingError((New-IslErrorRecord @recordSplat)) | ||
| } | ||
| try { | ||
| Start-ScheduledTask -TaskName $taskName | ||
|
|
@@ -228,7 +256,13 @@ | |
| 'account holds a session)' | ||
| } | ||
| else { '' } | ||
| throw "The scheduled task for $userName did not start: $code$hint" | ||
| $recordSplat = @{ | ||
| Message = "The scheduled task for $userName did not start: $code$hint" | ||
| ErrorId = 'ScheduledTaskNotStarted' | ||
| Category = [System.Management.Automation.ErrorCategory]::InvalidOperation | ||
| TargetObject = $userName | ||
| } | ||
| $PSCmdlet.ThrowTerminatingError((New-IslErrorRecord @recordSplat)) | ||
| } | ||
| if (-not (Test-Path -LiteralPath $exitFile)) { | ||
| $timedOut = $true | ||
|
|
@@ -256,6 +290,9 @@ | |
| } | ||
| while ($state -eq 'Running' -and (Get-Date) -lt $settleUntil) | ||
| } | ||
| } catch { | ||
| # The task that did not start, and a cleanup after a timeout that could not be run | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This |
||
| $PSCmdlet.ThrowTerminatingError($_) | ||
| } | ||
| finally { | ||
| # Of the task cmdlets used here only this one has -WhatIf. Under a preference the | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The message is the one the function had, so that the two existing tests of it, under error action
ContinueandStop, hold as they were.icacls.exewrites its reason to the error stream ("No mapping between account names and security IDs was done.") and a count of files to the output stream, so the message takes both, the reason first.