Repository navigation
fix(analysis)!: end the analysis and rule commands the cmdlet way #96
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: fix/terminating-errors-tenant
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 |
|---|---|---|
|
|
@@ -65,26 +65,36 @@ function Get-IslSetting { | |
| $context = $Table['Context'] | ||
| $architecture = $Table['Architecture'] | ||
| $severityTable = $Table['Severity'] | ||
| # The first value that is wrong is the one reported | ||
| $problem = $null | ||
|
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. Five |
||
| if ($minimumSeverity -and "$minimumSeverity" -notin $validSeverities) { | ||
| throw "${Source}: MinimumSeverity must be one of $($validSeverities -join ', ')" | ||
| } | ||
| if ($scriptType -and "$scriptType" -notin $validTypes) { | ||
| throw "${Source}: ScriptType must be one of $($validTypes -join ', ')" | ||
| } | ||
| if ($context -and "$context" -notin 'System', 'User') { | ||
| throw "${Source}: Context must be System or User" | ||
| } | ||
| if ($architecture -and "$architecture" -notin 'x86', 'x64', 'arm64') { | ||
| throw "${Source}: Architecture must be x86, x64 or arm64" | ||
| $problem = "MinimumSeverity must be one of $($validSeverities -join ', ')" | ||
| } elseif ($scriptType -and "$scriptType" -notin $validTypes) { | ||
| $problem = "ScriptType must be one of $($validTypes -join ', ')" | ||
| } elseif ($context -and "$context" -notin 'System', 'User') { | ||
| $problem = 'Context must be System or User' | ||
| } elseif ($architecture -and "$architecture" -notin 'x86', 'x64', 'arm64') { | ||
| $problem = 'Architecture must be x86, x64 or arm64' | ||
| } | ||
| $severity = @{} | ||
| $severityKeys = if ($severityTable) { @($severityTable.Keys | Where-Object { $null -ne $_ }) } else { @() } | ||
| foreach ($key in $severityKeys) { | ||
| if ("$($severityTable[$key])" -notin $validSeverities) { | ||
| throw "${Source}: Severity for $key must be one of $($validSeverities -join ', ')" | ||
| if ($null -eq $problem -and "$($severityTable[$key])" -notin $validSeverities) { | ||
| $problem = "Severity for $key must be one of $($validSeverities -join ', ')" | ||
| } | ||
| $severity[$key] = "$($severityTable[$key])" | ||
| } | ||
| if ($null -ne $problem) { | ||
| # This nested function has no $PSCmdlet of its own. The record is thrown, and the | ||
| # function that holds this one ends with it | ||
| $recordSplat = @{ | ||
| Message = "${Source}: $problem" | ||
| ErrorId = 'SettingsInvalid' | ||
| Category = [System.Management.Automation.ErrorCategory]::InvalidData | ||
| TargetObject = $Source | ||
| } | ||
| throw (New-IslErrorRecord @recordSplat) | ||
|
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 |
||
| } | ||
| [pscustomobject]@{ | ||
| PSTypeName = 'IntuneScriptLab.Settings' | ||
| Source = $Source | ||
|
|
@@ -102,29 +112,39 @@ function Get-IslSetting { | |
| } | ||
| } | ||
|
|
||
| if ($null -ne $Settings) { | ||
| if ($Settings -is [hashtable]) { return ConvertTo-SettingsObject -Table $Settings -Source '-Settings' } | ||
| $file = (Resolve-Path -LiteralPath "$Settings" -ErrorAction Stop).ProviderPath | ||
| return ConvertTo-SettingsObject -Table (Import-PowerShellDataFile -LiteralPath $file) -Source $file | ||
| } | ||
|
|
||
| $resolved = (Resolve-Path -LiteralPath $Path -ErrorAction Stop).ProviderPath | ||
| $folder = if (Test-Path -LiteralPath $resolved -PathType Container) { $resolved } | ||
| else { Split-Path -Path $resolved -Parent } | ||
| if ($Cache.ContainsKey($folder)) { return $Cache[$folder] } | ||
|
|
||
| # A settings file that cannot be read, and a value in it that is wrong, end the function | ||
| $found = $null | ||
| $probe = $folder | ||
| while ($probe) { | ||
| $candidate = Join-Path -Path $probe -ChildPath 'IntuneScriptLab.settings.psd1' | ||
| if (Test-Path -LiteralPath $candidate -PathType Leaf) { | ||
| $candidateTable = Import-PowerShellDataFile -LiteralPath $candidate | ||
| $found = ConvertTo-SettingsObject -Table $candidateTable -Source $candidate | ||
| break | ||
| try { | ||
| if ($Settings -is [hashtable]) { | ||
| $found = ConvertTo-SettingsObject -Table $Settings -Source '-Settings' | ||
| } elseif ($null -ne $Settings) { | ||
| $file = (Resolve-Path -LiteralPath "$Settings" -ErrorAction Stop).ProviderPath | ||
| $fileTable = Import-PowerShellDataFile -LiteralPath $file -ErrorAction Stop | ||
| $found = ConvertTo-SettingsObject -Table $fileTable -Source $file | ||
| } else { | ||
| $resolved = (Resolve-Path -LiteralPath $Path -ErrorAction Stop).ProviderPath | ||
| $folder = if (Test-Path -LiteralPath $resolved -PathType Container) { $resolved } | ||
| else { Split-Path -Path $resolved -Parent } | ||
| if ($Cache.ContainsKey($folder)) { | ||
| $found = $Cache[$folder] | ||
| } else { | ||
| $probe = $folder | ||
| while ($probe) { | ||
| $candidate = Join-Path -Path $probe -ChildPath 'IntuneScriptLab.settings.psd1' | ||
| if (Test-Path -LiteralPath $candidate -PathType Leaf) { | ||
| $candidateTable = Import-PowerShellDataFile -LiteralPath $candidate -ErrorAction Stop | ||
| $found = ConvertTo-SettingsObject -Table $candidateTable -Source $candidate | ||
| break | ||
| } | ||
| $probe = Split-Path -Path $probe -Parent | ||
| } | ||
| if (-not $found) { $found = ConvertTo-SettingsObject -Table @{} -Source '' } | ||
| $Cache[$folder] = $found | ||
| } | ||
| } | ||
| $probe = Split-Path -Path $probe -Parent | ||
| } catch { | ||
| $PSCmdlet.ThrowTerminatingError($_) | ||
| } | ||
| if (-not $found) { $found = ConvertTo-SettingsObject -Table @{} -Source '' } | ||
| $Cache[$folder] = $found | ||
|
|
||
| $found | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,9 +3,37 @@ | |
| # Should-* aliases (Set-Alias in the module file). Each fails with the diagnosis Intune would give. | ||
|
|
||
| function Test-IslPesterAssertionSupport { | ||
| # New-ShouldAssertion arrived in Pester 6.2; without it these functions cannot report properly. | ||
| <# | ||
| .SYNOPSIS | ||
| Ends with an error when the Pester that is loaded cannot carry the assertions. | ||
|
|
||
| .DESCRIPTION | ||
| New-ShouldAssertion arrived in Pester 6.2; without it the Assert- functions of this file | ||
| cannot report a failure properly. Returns nothing when the command is there. | ||
|
|
||
| .EXAMPLE | ||
| Test-IslPesterAssertionSupport | ||
|
|
||
| Nothing with Pester 6.2 or later loaded, and a terminating error otherwise. | ||
|
|
||
| .INPUTS | ||
| None. | ||
|
|
||
| .OUTPUTS | ||
| None. | ||
| #> | ||
| [CmdletBinding()] | ||
| [OutputType([void])] | ||
| param() | ||
|
|
||
| if (-not (Get-Command -Name New-ShouldAssertion -ErrorAction SilentlyContinue)) { | ||
| throw 'The IntuneScriptLab assertions need Pester 6.2 or later loaded (Import-Module Pester)' | ||
| $recordSplat = @{ | ||
| Message = 'The IntuneScriptLab assertions need Pester 6.2 or later loaded (Import-Module Pester)' | ||
| ErrorId = 'PesterAssertionNotSupported' | ||
| Category = [System.Management.Automation.ErrorCategory]::NotInstalled | ||
| TargetObject = 'Pester' | ||
| } | ||
| $PSCmdlet.ThrowTerminatingError((New-IslErrorRecord @recordSplat)) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -30,7 +58,11 @@ function Assert-HaveIntuneStatus { | |
| [string]$Because | ||
| ) | ||
|
|
||
| Test-IslPesterAssertionSupport | ||
| try { | ||
| Test-IslPesterAssertionSupport | ||
| } catch { | ||
| $PSCmdlet.ThrowTerminatingError($_) | ||
| } | ||
| $assert = New-ShouldAssertion -Caller $PSCmdlet -Actual $Actual -Buffer $Input | ||
| $Actual = $assert.Actual() | ||
|
|
||
|
|
@@ -67,7 +99,11 @@ function Assert-BeIntuneDetected { | |
| [string]$Because | ||
| ) | ||
|
|
||
| Test-IslPesterAssertionSupport | ||
| try { | ||
| Test-IslPesterAssertionSupport | ||
| } catch { | ||
| $PSCmdlet.ThrowTerminatingError($_) | ||
| } | ||
| $assert = New-ShouldAssertion -Caller $PSCmdlet -Actual $Actual -Buffer $Input | ||
| $Actual = $assert.Actual() | ||
|
|
||
|
|
@@ -95,7 +131,11 @@ function Assert-NotBeIntuneDetected { | |
| [string]$Because | ||
| ) | ||
|
|
||
| Test-IslPesterAssertionSupport | ||
| try { | ||
| Test-IslPesterAssertionSupport | ||
| } catch { | ||
| $PSCmdlet.ThrowTerminatingError($_) | ||
| } | ||
| $assert = New-ShouldAssertion -Caller $PSCmdlet -Actual $Actual -Buffer $Input | ||
| $Actual = $assert.Actual() | ||
|
|
||
|
|
@@ -127,7 +167,11 @@ function Assert-HaveIntuneRunState { | |
| [string]$Because | ||
| ) | ||
|
|
||
| Test-IslPesterAssertionSupport | ||
| try { | ||
| Test-IslPesterAssertionSupport | ||
| } catch { | ||
| $PSCmdlet.ThrowTerminatingError($_) | ||
| } | ||
| $assert = New-ShouldAssertion -Caller $PSCmdlet -Actual $Actual -Buffer $Input | ||
| $Actual = $assert.Actual() | ||
|
|
||
|
|
@@ -165,7 +209,11 @@ function Assert-PassIntuneAnalysis { | |
| [string]$Because | ||
| ) | ||
|
|
||
| Test-IslPesterAssertionSupport | ||
| try { | ||
| Test-IslPesterAssertionSupport | ||
| } catch { | ||
| $PSCmdlet.ThrowTerminatingError($_) | ||
| } | ||
| # A file, a folder, or a pipeline of either: every script found is analyzed and the failure | ||
| # names each file | ||
| $assert = New-ShouldAssertion -Caller $PSCmdlet -Actual $Actual -Buffer $Input | ||
|
|
@@ -174,8 +222,17 @@ function Assert-PassIntuneAnalysis { | |
| if ($item -is [System.IO.FileSystemInfo]) { $item.FullName } else { "$item" } | ||
| }) | ||
|
|
||
| $findings = @(Test-IntuneScript -Path $paths -ScriptType $ScriptType -MinimumSeverity $MinimumSeverity | | ||
| Where-Object { $_.RuleName -ne 'IslAssumedContext' }) | ||
| # A path that cannot be analyzed fails the assertion with its error. Test-IntuneScript reports | ||
| # such a path and goes on, and with nothing analyzed there is no finding: the assertion | ||
| # would pass | ||
| $analysisSplat = @{ | ||
| Path = $paths; ScriptType = $ScriptType; MinimumSeverity = $MinimumSeverity; ErrorAction = 'Stop' | ||
|
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. Without |
||
| } | ||
| try { | ||
| $findings = @(Test-IntuneScript @analysisSplat | Where-Object { $_.RuleName -ne 'IslAssumedContext' }) | ||
| } catch { | ||
| $PSCmdlet.ThrowTerminatingError($_) | ||
| } | ||
| if ($findings.Count -eq 0) { return } | ||
|
|
||
| $list = ($findings | ForEach-Object { | ||
|
|
@@ -205,7 +262,11 @@ function Assert-BeIntuneApplicable { | |
| [string]$Because | ||
| ) | ||
|
|
||
| Test-IslPesterAssertionSupport | ||
| try { | ||
| Test-IslPesterAssertionSupport | ||
| } catch { | ||
| $PSCmdlet.ThrowTerminatingError($_) | ||
| } | ||
| $assert = New-ShouldAssertion -Caller $PSCmdlet -Actual $Actual -Buffer $Input | ||
| $Actual = $assert.Actual() | ||
|
|
||
|
|
@@ -233,7 +294,11 @@ function Assert-NotBeIntuneApplicable { | |
| [string]$Because | ||
| ) | ||
|
|
||
| Test-IslPesterAssertionSupport | ||
| try { | ||
| Test-IslPesterAssertionSupport | ||
| } catch { | ||
| $PSCmdlet.ThrowTerminatingError($_) | ||
| } | ||
| $assert = New-ShouldAssertion -Caller $PSCmdlet -Actual $Actual -Buffer $Input | ||
| $Actual = $assert.Actual() | ||
|
|
||
|
|
||
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 preference this script sets at its top reaches the module only when the script runs in the global scope, as it does when started with
-File. Called with&from another script it does not, and the gate went on to print0 finding(s)and exit 0 for a folder whose script was never analyzed. The new test inScriptGate.Tests.ps1starts the gate that way for this reason.