Repository navigation
Conversation
Test-IntuneDeployedScript, Compare-IntuneDeployedScript, Get-IntuneScriptHealth and Test-IntuneAssignmentFilter, and the Graph seam and the export reader below them, ended on a failure with throw (ERR-10, ERR-11, ERR-14). Each failure the module detects is now an error record with an ID and a category, raised with ThrowTerminatingError, and every call of a function that can end that way is inside a try with a catch. Invoke-IslGraphRequest ends with the error Graph gave, unchanged, so a caller still reads the status code and the response from it. The tenant commands end with it in turn. The two commands that write one result after another read each policy inside a try and write what it gave after that try (ERR-15), so the output is still a stream and an error of a command further down the pipeline is not caught as theirs. Get-IslExportReport is one try from the job to the rows, and its download has -ErrorAction Stop. Get-IntuneScriptHealth returned its reports when only the file of -MarkdownPath could not be written, because a .NET exception ends just the statement. It still does, now on purpose: the failed write is reported with WriteError and -ErrorAction Stop ends the command. The contract test's list of calls that still rely on a throw loses the twenty of this area; thirteen remain, all in the analysis and rule commands. Refs #64. BREAKING CHANGE: a script that calls one of the four tenant commands without try and without $ErrorActionPreference = 'Stop' goes on at its next statement after a failure, where it used to end. Set the preference, or make the call inside try.
fadwen
left a comment
There was a problem hiding this comment.
Notes on the lines whose reason the diff does not show.
| foreach ($summary in $summaries) { | ||
| # A read that fails ends the command. What one policy gives is written after its try, so | ||
| # that an error of a command further down the pipeline is not caught here | ||
| $policyOutput = try { |
There was a problem hiding this comment.
Most of the diff of this file and of Compare-IntuneDeployedScript.ps1 is this: the body of each loop moved four spaces to the right, into a try whose output is assigned. Hide whitespace to see what else changed. The alternative with no indentation at all was to assign the output of the outer try and write it after the finally, which would have turned the stream into one batch at the end of a run that can take minutes.
| } | ||
| Test-Assignment -PolicyKind 'Remediation' -Policy $policy -RunAsAccount $runAsAccount | ||
| Test-AssignmentFilter -PolicyKind 'Remediation' -Policy $policy | ||
| $policyOutput |
There was a problem hiding this comment.
Written after the try, not inside it (ERR-15). A caller's | ForEach-Object { throw ... } raises its error at this statement, where no catch of this command is in the way; the test "does not take the error of a command further down the pipeline for its own" holds that.
| Test-PolicyScript @remediateSplat @policySettings | ||
| } | ||
| else { | ||
| $detectOnlyEvidence = ('A remediation created without remediationScriptContent ran ' + |
There was a problem hiding this comment.
The text is unchanged. It moved out of the hashtable because, four spaces further right, it needed a third line, and each continuation line inside a hashtable entry is one more indentation finding for #73 to clear. docs/Rules.md is built from Private/Rules only, so it does not read this.
| $policyOutput = try { | ||
| $summaryId = Get-IslPropertyValue -InputObject $summary -Name 'id' | ||
| $policy = Invoke-IslGraphRequest -Uri "$remediations/$summaryId" | ||
| $enforceSignature = Get-IslPropertyValue -InputObject $policy -Name 'enforceSignatureCheck' |
There was a problem hiding this comment.
Moved out of the hashtable below for its length alone: inside the try the entry had 119 characters, and CI refuses more than 115. The same was done in three other places.
| ) | ||
| foreach ($counter in $counters) { | ||
| $runCount[$counter] = [int](Get-IslPropertyValue -InputObject $run -Name "${counter}DeviceCount") | ||
| # A policy that cannot be read ends the command: a report with gaps would read as health |
There was a problem hiding this comment.
One try for all three kinds here, where the two commands above have one for each policy. This command already collected its reports in a list, because the Markdown file needs all of them, so nothing is lost by it.
| } catch { | ||
| # The reports were read and are returned below. A file that could not be written is | ||
| # reported, and the caller decides with -ErrorAction whether that ends the command | ||
| $PSCmdlet.WriteError($_) |
There was a problem hiding this comment.
The one place in this pull request where a failure does not end the command, and it is the behaviour main has by accident: a .NET exception ends only the statement, so the reports were returned with the error beside them. Measured on main and here with a folder that does not exist: 1 report and one DirectoryNotFoundException in both.
| $PSCmdlet.ThrowTerminatingError($_) | ||
| } | ||
|
|
||
| if (-not $All) { return $reply } |
There was a problem hiding this comment.
The reply is held in a variable for the length of the try and returned here. For a reply that is one object, a hashtable or the text of a $count, that is what return Invoke-Once ... gave. The comparison against the lab tenant is the check that nothing changed for real replies.
| $PSCmdlet.ThrowTerminatingError((New-IslErrorRecord @recordSplat -TargetObject $ReportName)) | ||
| } | ||
|
|
||
| Invoke-WebRequest -Uri $jobUrl -OutFile $zip -UseBasicParsing -ErrorAction Stop |
There was a problem hiding this comment.
-ErrorAction Stop is new (ERR-12). Without it a download that failed was reported and the function went on to open a zip that was not there. The request carries no credential, the address is a signed one from the job, so SEC-09's -MaximumRedirection 0 does not apply.
| Import-Module (Join-Path $script:ModuleRoot 'IntuneScriptLab.psd1') -Force | ||
| . (Join-Path $script:ModuleRoot 'Tests\TestHelpers\TestHelpers.ps1') | ||
| # Stands in for the Graph module in a calling script: every request fails | ||
| $script:GraphDown = 'function global:Invoke-MgGraphRequest { ' + |
There was a problem hiding this comment.
A function named Invoke-MgGraphRequest in the calling script, because a mock does not reach a new process. A function is found before a module's command of the same name, so the case does the same on a computer that has Microsoft.Graph.Authentication installed and on a runner that has not.
Summary
The tenant commands are the third area of #64.
Test-IntuneDeployedScript,Compare-IntuneDeployedScript,Get-IntuneScriptHealthandTest-IntuneAssignmentFilter, and the Graph seam and the export reader below them, ended on a failure withthrow(ERR-10, ERR-11, ERR-14). Each failure the module detects is now an error record with an ID and a category, raised with$PSCmdlet.ThrowTerminatingError(), and every call of a function that can end that way is inside atrywith acatch.This pull request is stacked on #92 and merges after it.
This changes what a calling script sees. A script that calls one of the four commands without
tryand without$ErrorActionPreference = 'Stop'goes on at its next statement after a failure, where it used to end. What the commands return for a tenant they can read is unchanged.Changes
Invoke-IslGraphRequestends with the error Graph gave, unchanged, so a caller still reads the status code and the response from it; the retry logic is as it was. The module's own check has the IDGraphModuleNotLoaded.Get-IslExportReportis onetryfrom the export job to the rows, with-ErrorAction Stopon the download (ERR-12). Its three failures have the IDsExportJobTimedOut,ExportJobFailedandExportHasNoCsv.Test-IntuneDeployedScriptandCompare-IntuneDeployedScriptwrite one result after another. Each list of policies is read inside atry, each policy is read and judged inside atry, and what the policy gave is written after thattry(ERR-15). The output stays a stream, and an error of a command further down the pipeline is not caught as theirs. The outertrywith thefinallythat removes the work folder is unchanged.Compare-IntuneDeployedScripthas the IDLocalPathNotFolderfor a-Paththat is a file.Get-IntuneScriptHealthcollects its reports for the Markdown file, so its two reading parts are each onetry. A file of-MarkdownPaththat cannot be written is reported withWriteError, and the reports are still returned; see the first note below.Test-IntuneAssignmentFilterends inbeginwhen the facts of this device cannot be read (DeviceFactsNeedWindowson another operating system), andprocessdoes not run.Tests/Unit/Module.ErrorFlow.Tests.ps1: the list of calls that still rely on athrowgoes from 33 to 13. The 13 are all in the analysis and rule commands.tryin a new process, with a stand-in forInvoke-MgGraphRequestthat fails every request.docs/IntuneScriptLab/with the rebuilten-US/IntuneScriptLab-Help.xml, and the Graph paragraph ofCLAUDE.md.tryindented them and were split. The read ofenforceSignatureCheckmoved out of the hashtable that held it, in four places.Verification
Test-IntuneDeployedScript,Compare-IntuneDeployedScriptandGet-IntuneScriptHealthwere run frommainand from this branch against the same tenant, with GET requests only (-SkipRunStatefor the apps, whose install counts need an export job). Both gave 1,027 findings, 183 drift results and 151 health reports, and the two JSON files of 647,685 characters are identical.try, every Graph request failing, on PowerShell 7.6.10 and Windows PowerShell 5.1: each of the three commands leaves one error,Graph is down, returns nothing, and the script reaches its end. Onmainthe script ends at the command../Tests/Invoke-Tests.ps1 -CodeCoverageon PowerShell 7.6.10: 1,142 passed, 7 skipped, 95.27 percent coverage. 3 failed, the three that fail on this computer onmainas well (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)../Tests/Invoke-Tests.ps1on Windows PowerShell 5.1.26100: 1,106 passed, 43 skipped, the same 3 failed.PublicandPrivate: the 6 ERR-11 findings are gone, one STY-02 with them, and nothing is added../Build/Publish-Module.ps1 -WhatIf: staged, imported in a new process and verified; nothing published.quality-gates.yml, started by hand on this branch because the pull request is not based onmain: run 45310065917 passed all five jobs.Notes
main,Get-IntuneScriptHealth -MarkdownPathwith a folder that does not exist returns its reports and leaves aDirectoryNotFoundExceptionbehind, because a .NET exception ends only the statement. Putting the write inside thetryof the reads would have ended the command without the reports, after everything had been read. The write has atryof its own instead, and the behaviour is the onemainhas, now stated in the code, the help and a test.Test-PolicyScriptcallsTest-IntuneScriptwithout-ErrorAction Stop. That is right whileTest-IntuneScriptends on a failure; the analysis area, next, has to revisit it if that command starts to report a file it cannot read without ending.