diff --git a/CHANGELOG.md b/CHANGELOG.md index bf90844..be17128 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -153,6 +153,10 @@ entries are carried over from the manifest's release notes. ### Fixed +- A `Severity` setting that is not a hashtable, such as `Severity = 'Error'` in a settings file + or in `-Settings`, ends `Test-IntuneScript` with `SettingsInvalid` and a message that names the + source. It ended with a strict-mode error, `PropertyNotFoundStrict`, that named neither the + source nor the setting (#105). - `Export-IntuneAgentDiagnostic` left every registry value whose name starts with `PS`, such as `PSVersion`, out of the registry text. Only the five properties that the PowerShell provider adds to each key are left out now. diff --git a/Private/ConvertTo-IslSettingsObject.ps1 b/Private/ConvertTo-IslSettingsObject.ps1 index 6872448..b32ca54 100644 --- a/Private/ConvertTo-IslSettingsObject.ps1 +++ b/Private/ConvertTo-IslSettingsObject.ps1 @@ -8,7 +8,8 @@ function ConvertTo-IslSettingsObject { -Settings. Every key is optional. A key the module does not know is named in a warning and otherwise ignored. A value outside its set ends the function with the error SettingsInvalid, which names the source and the first value that is wrong, checked in this order: - MinimumSeverity, ScriptType, Context, Architecture, then the entries of Severity. + MinimumSeverity, ScriptType, Context, Architecture, Severity, which must be a hashtable, then + the entries of Severity. .PARAMETER Table The settings, in the shape of a settings file. @@ -68,9 +69,14 @@ function ConvertTo-IslSettingsObject { $problem = 'Context must be System or User' } elseif ($architecture -and "$architecture" -notin 'x86', 'x64', 'arm64') { $problem = 'Architecture must be x86, x64 or arm64' + } elseif ($severityTable -and $severityTable -isnot [System.Collections.IDictionary]) { + $problem = 'Severity must be a hashtable of rule names and severities' } $severity = @{} - $severityKeys = if ($severityTable) { @($severityTable.Keys | Where-Object { $null -ne $_ }) } else { @() } + $severityKeys = @() + if ($severityTable -is [System.Collections.IDictionary]) { + $severityKeys = @($severityTable.Keys | Where-Object { $null -ne $_ }) + } foreach ($key in $severityKeys) { if ($null -eq $problem -and "$($severityTable[$key])" -notin $validSeverities) { $problem = "Severity for $key must be one of $($validSeverities -join ', ')" diff --git a/Tests/Unit/Private/ConvertTo-IslSettingsObject.Tests.ps1 b/Tests/Unit/Private/ConvertTo-IslSettingsObject.Tests.ps1 index 7a4fb0d..664db7a 100644 --- a/Tests/Unit/Private/ConvertTo-IslSettingsObject.Tests.ps1 +++ b/Tests/Unit/Private/ConvertTo-IslSettingsObject.Tests.ps1 @@ -89,6 +89,14 @@ Describe 'ConvertTo-IslSettingsObject' -Tag 'Unit', 'Private' { Case = 'a severity override outside the set'; Table = @{ Severity = @{ IslLongSleep = 'Loud' } } Text = '*Severity for IslLongSleep*' } + @{ + Case = 'a severity that is not a hashtable'; Table = @{ Severity = 'Error' } + Text = '*Severity must be a hashtable of rule names*' + } + @{ + Case = 'a severity that is a list'; Table = @{ Severity = @('IslLongSleep', 'Error') } + Text = '*Severity must be a hashtable of rule names*' + } ) { $failure = { ConvertTo-SettingsTable -Table $Table -Source 'C:\repo\IntuneScriptLab.settings.psd1' } | Should-Throw -ExceptionMessage $Text diff --git a/Tests/Unit/Public/Test-IntuneScript.Tests.ps1 b/Tests/Unit/Public/Test-IntuneScript.Tests.ps1 index a58e04c..a22728f 100644 --- a/Tests/Unit/Public/Test-IntuneScript.Tests.ps1 +++ b/Tests/Unit/Public/Test-IntuneScript.Tests.ps1 @@ -93,6 +93,19 @@ Describe 'Test-IntuneScript' -Tag 'Unit', 'Public' { Should-Invoke Get-IslScriptContext -ModuleName IntuneScriptLab -Times 0 -Exactly } + It 'reports a settings file whose Severity is not a hashtable with SettingsInvalid, naming the file' { + $scriptPath = New-TestScript 'severitytext\Detect-One.ps1' 'exit 2' + $settingsFile = Join-Path (Split-Path $scriptPath -Parent) 'IntuneScriptLab.settings.psd1' + Set-Content -LiteralPath $settingsFile -Value "@{ Severity = 'Error' }" -Encoding ascii + + $output = Test-IntuneScript -Path $scriptPath -ErrorAction Continue 2>&1 + $errors = @($output | Where-Object { $_ -is [System.Management.Automation.ErrorRecord] }) + + $errors | Should-BeCollection -Count 1 + $errors[0].FullyQualifiedErrorId | Should-BeLikeString 'SettingsInvalid*' + $errors[0].Exception.Message | Should-BeLikeString "$settingsFile*Severity must be a hashtable*" + } + It 'leaves a calling script one error for a path that is not there, and the findings of the other' { $good = New-TestScript 'caller\Detect-Good.ps1' 'exit 2' $missing = Join-Path $TestDrive 'caller\missing.ps1'