Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ entries are carried over from the manifest's release notes.
### Changed

- The helper functions nested in `Test-IntuneScript`, `Test-IntuneWin32Requirement`,
`Export-IntuneFindingSarif`, `Export-IntuneAgentDiagnostic`, `Get-IslSetting`,
`Get-IslAgentLogEvent` and `Invoke-IslGraphRequest` are private functions of their own, each an
`Test-IntuneAssignmentFilter`, `Export-IntuneFindingSarif`, `Export-IntuneAgentDiagnostic`,
`Get-IslSetting`, `Get-IslAgentLogEvent` and `Invoke-IslGraphRequest` are private functions of their own, each an

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit (optional): this line is 117 characters, and the rest of the entry is wrapped at about 100. Fix: rewrap the paragraph so that "are private functions of their own, each an" moves to the next line.

advanced function with help and tests (#65). Nothing changes for a caller: a settings value that
is not allowed still ends `Test-IntuneScript` with `SettingsInvalid,Test-IntuneScript`.
- The README's section headings no longer carry the version that introduced each feature, the
Expand Down
51 changes: 51 additions & 0 deletions Private/Compare-IslFilterVersion.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
function Compare-IslFilterVersion {
<#
.SYNOPSIS
Compares two version texts the way the assignment filter evaluator ordered them.

.DESCRIPTION
Both texts are read as four numbers by Get-IslVersionPart and compared part by part, so
'10.0' equals '10.0.0.0' and '10.0.9' is less than '10.0.10'. When either text is not a
version there is no order, and the function returns nothing; the filter's gt, ge, lt and le
then do not match.

.PARAMETER Actual
The value the device has.

.PARAMETER Expected
The value of the rule.

.EXAMPLE
Compare-IslFilterVersion -Actual '10.0.26100' -Expected '10.0.22631'

Returns 1: the device's version is the greater.

.INPUTS
None.

.OUTPUTS
System.Int32. -1, 0 or 1, or nothing when a side is not a version.
#>
[CmdletBinding()]
[OutputType([int])]
param(
[Parameter(Mandatory)]
[AllowEmptyString()]
[string]$Actual,

[Parameter(Mandatory)]
[AllowEmptyString()]
[string]$Expected
)

$left = @(Get-IslVersionPart -Text $Actual)
$right = @(Get-IslVersionPart -Text $Expected)
if ($left.Count -ne 4 -or $right.Count -ne 4) {
return
}
for ($index = 0; $index -lt 4; $index++) {
if ($left[$index] -lt $right[$index]) { return -1 }
if ($left[$index] -gt $right[$index]) { return 1 }
}
0
}
40 changes: 40 additions & 0 deletions Private/Get-IslVersionPart.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
function Get-IslVersionPart {
<#
.SYNOPSIS
Splits a version text into four numbers, the way an assignment filter compares versions.

.DESCRIPTION
A text of one to four numbers separated by dots, with white space around it allowed, gives
four numbers, the parts that are missing as 0: '10.0.26100' gives 10, 0, 26100, 0. Any other
text is not a version, and gives nothing.

.PARAMETER Text
The text to read.

.EXAMPLE
Get-IslVersionPart -Text '10.0.26100.4202'

Returns 10, 0, 26100 and 4202.

.INPUTS
None.

.OUTPUTS
System.Int64. Four numbers, or nothing when the text is not a version.
#>
[CmdletBinding()]
[OutputType([long])]
param(
[Parameter(Mandatory)]
[AllowEmptyString()]
[string]$Text
)

if ($Text -notmatch '^\s*\d+(\.\d+){0,3}\s*$') {
return
}
$parts = [System.Collections.Generic.List[long]]::new()
foreach ($part in ($Text.Trim() -split '\.')) { $parts.Add([long]$part) }

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pre-existing (not from this change, not blocking): a version part too large for [long] gets silently cut short. The regex lets \d+ of any length through, so in '10.99999999999999999999' the [long] cast throws partway through. That error ends only the foreach statement, the while fills the gap with zeros, and the function returns 10, 0, 0, 0. Measured under strict mode: count 4, values 10,0,0,0. A device or rule value like that then counts as equal to 10.0, and gt, ge, lt and le give wrong results instead of "not a version". Fix: treat a part that [long]::TryParse rejects as not a version and return nothing, and add '10.99999999999999999999' to the "gives nothing" cases.

while ($parts.Count -lt 4) { $parts.Add(0) }
$parts.ToArray()
}
71 changes: 71 additions & 0 deletions Private/Test-IslFilterClause.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
function Test-IslFilterClause {
<#
.SYNOPSIS
Tells whether one clause of an assignment filter rule matches the facts of a device.

.DESCRIPTION
The clause is one comparison that ConvertFrom-IslFilterRule read, such as
(device.osVersion -ge "10.0.22000"). Its property is looked up in the facts; a property the
device does not have compares as an empty text. Text comparisons with eq, ne, in and notIn
are those of PowerShell, which ignore letter case, and startsWith, contains and notContains
ignore it as well. A clause of the Version kind compares with Compare-IslFilterVersion for
eq, ne, gt, ge, lt and le; when one of its sides is not a version, eq does not match, ne
does, and gt, ge, lt and le do not.

The clause is changed in place: Actual is set to the device's value, or $null, and Matched
to the result, so that the result object of Test-IntuneAssignmentFilter shows what each
clause saw.

.PARAMETER Clause
The clause, an IntuneScriptLab.FilterClause with Property, Operator, Value and Kind.

.PARAMETER Facts
The device's facts, by property name, compared without regard to letter case.

.EXAMPLE
Test-IslFilterClause -Clause $parsed.Clauses[0] -Facts $facts

$true when the device's value satisfies the first clause of the rule.

.INPUTS
None.

.OUTPUTS
System.Boolean.
#>
[CmdletBinding()]
[OutputType([bool])]
param(
[Parameter(Mandatory)]
[PSTypeName('IntuneScriptLab.FilterClause')]
[psobject]$Clause,

[Parameter(Mandatory)]
[AllowEmptyCollection()]
[System.Collections.Generic.Dictionary[string, object]]$Facts

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit (optional): the help says the facts are "compared without regard to letter case", but nothing in this function makes that true. It depends on the comparer of the dictionary the caller built. A caller that passes [Dictionary[string, object]]::new() gets case-sensitive lookups, so device.DeviceName finds no deviceName fact and compares as an empty text, with no error. The same sentence is in Test-IslFilterNode's help. Fix: say in both helps that the caller must build the dictionary with [System.StringComparer]::OrdinalIgnoreCase, or check $Facts.Comparer and fail if it is not that one.

)

$raw = if ($Facts.ContainsKey($Clause.Property)) { $Facts[$Clause.Property] } else { $null }
$actual = if ($null -eq $raw) { '' } else { "$raw" }
$Clause.Actual = if ($null -eq $raw) { $null } else { "$raw" }
$values = @(foreach ($item in @($Clause.Value)) { if ($null -ne $item) { "$item".Trim() } })
$expected = if ($values.Count) { $values[0] } else { '' }
$ignoreCase = [System.StringComparison]::OrdinalIgnoreCase
$order = $null
if ($Clause.Kind -eq 'Version') { $order = Compare-IslFilterVersion -Actual $actual -Expected $expected }
$matched = switch ($Clause.Operator) {
'eq' { if ($Clause.Kind -eq 'Version') { $order -eq 0 } else { $actual -eq $expected } }
'ne' { if ($Clause.Kind -eq 'Version') { $order -ne 0 } else { $actual -ne $expected } }
'in' { $values -contains $actual }
'notIn' { $values -notcontains $actual }
'startsWith' { $actual.StartsWith($expected, $ignoreCase) }
'contains' { $actual.IndexOf($expected, $ignoreCase) -ge 0 }
'notContains' { $actual.IndexOf($expected, $ignoreCase) -lt 0 }
'gt' { $null -ne $order -and $order -gt 0 }
'ge' { $null -ne $order -and $order -ge 0 }
'lt' { $null -ne $order -and $order -lt 0 }
'le' { $null -ne $order -and $order -le 0 }
}
$Clause.Matched = [bool]$matched
$Clause.Matched
}
54 changes: 54 additions & 0 deletions Private/Test-IslFilterNode.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
function Test-IslFilterNode {
<#
.SYNOPSIS
Tells whether a node of a parsed assignment filter rule matches the facts of a device.

.DESCRIPTION
The node is one element of the tree that ConvertFrom-IslFilterRule builds: a Clause, or an
And or an Or of a Left and a Right node. A clause is tested with Test-IslFilterClause. Both
sides of And and Or are always tested, without stopping at the first that decides, so that
every clause of the rule records what it saw.

.PARAMETER Node
The node, a hashtable with Type, and Clause or Left and Right.

.PARAMETER Facts
The device's facts, by property name, compared without regard to letter case.

.EXAMPLE
Test-IslFilterNode -Node $parsed.Tree -Facts $facts

$true when the whole rule matches the device.

.INPUTS
None.

.OUTPUTS
System.Boolean.
#>
[CmdletBinding()]
[OutputType([bool])]
param(
[Parameter(Mandatory)]
[hashtable]$Node,

[Parameter(Mandatory)]
[AllowEmptyCollection()]
[System.Collections.Generic.Dictionary[string, object]]$Facts
)

switch ($Node['Type']) {
'Clause' { Test-IslFilterClause -Clause $Node['Clause'] -Facts $Facts }
'And' {
# Both sides run so every clause reports what it saw
$left = Test-IslFilterNode -Node $Node['Left'] -Facts $Facts
$right = Test-IslFilterNode -Node $Node['Right'] -Facts $Facts
$left -and $right
}
'Or' {
$left = Test-IslFilterNode -Node $Node['Left'] -Facts $Facts
$right = Test-IslFilterNode -Node $Node['Right'] -Facts $Facts
$left -or $right
}
}
}
70 changes: 1 addition & 69 deletions Public/Test-IntuneAssignmentFilter.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -43,74 +43,6 @@ function Test-IntuneAssignmentFilter {
$view = [ordered]@{}
foreach ($key in @($facts.Keys | Sort-Object)) { $view[$key] = $facts[$key] }
$deviceView = [pscustomobject]$view

function Get-VersionPart {
# Four numeric parts, missing ones as 0; $null when the text is not a version
param([string]$Text)
if ($Text -notmatch '^\s*\d+(\.\d+){0,3}\s*$') { return $null }
$parts = [System.Collections.Generic.List[long]]::new()
foreach ($part in ($Text.Trim() -split '\.')) { $parts.Add([long]$part) }
while ($parts.Count -lt 4) { $parts.Add(0) }
$parts.ToArray()
}

function Compare-Version {
# -1, 0 or 1 the way the filter evaluator ordered versions; $null when a side is not one
param([string]$Actual, [string]$Expected)
$left = Get-VersionPart -Text $Actual
$right = Get-VersionPart -Text $Expected
if ($null -eq $left -or $null -eq $right) { return $null }
for ($index = 0; $index -lt 4; $index++) {
if ($left[$index] -lt $right[$index]) { return -1 }
if ($left[$index] -gt $right[$index]) { return 1 }
}
0
}

function Test-Clause {
param($Clause)
$raw = if ($facts.ContainsKey($Clause.Property)) { $facts[$Clause.Property] } else { $null }
$actual = if ($null -eq $raw) { '' } else { "$raw" }
$Clause.Actual = if ($null -eq $raw) { $null } else { "$raw" }
$values = @(foreach ($item in @($Clause.Value)) { if ($null -ne $item) { "$item".Trim() } })
$expected = if ($values.Count) { $values[0] } else { '' }
$ignoreCase = [System.StringComparison]::OrdinalIgnoreCase
$order = $null
if ($Clause.Kind -eq 'Version') { $order = Compare-Version -Actual $actual -Expected $expected }
$matched = switch ($Clause.Operator) {
'eq' { if ($Clause.Kind -eq 'Version') { $order -eq 0 } else { $actual -eq $expected } }
'ne' { if ($Clause.Kind -eq 'Version') { $order -ne 0 } else { $actual -ne $expected } }
'in' { $values -contains $actual }
'notIn' { $values -notcontains $actual }
'startsWith' { $actual.StartsWith($expected, $ignoreCase) }
'contains' { $actual.IndexOf($expected, $ignoreCase) -ge 0 }
'notContains' { $actual.IndexOf($expected, $ignoreCase) -lt 0 }
'gt' { $null -ne $order -and $order -gt 0 }
'ge' { $null -ne $order -and $order -ge 0 }
'lt' { $null -ne $order -and $order -lt 0 }
'le' { $null -ne $order -and $order -le 0 }
}
$Clause.Matched = [bool]$matched
$Clause.Matched
}

function Test-Node {
param($Node)
switch ($Node.Type) {
'Clause' { Test-Clause -Clause $Node.Clause }
'And' {
# Both sides run so every clause reports what it saw
$left = Test-Node -Node $Node.Left
$right = Test-Node -Node $Node.Right
$left -and $right
}
'Or' {
$left = Test-Node -Node $Node.Left
$right = Test-Node -Node $Node.Right
$left -or $right
}
}
}
}

process {
Expand Down Expand Up @@ -139,7 +71,7 @@ function Test-IntuneAssignmentFilter {
$reason = "The rule is valid: $clauseCount clause(s), $(@($parsed.Warnings).Count) warning(s)"
}
else {
$matched = [bool](Test-Node -Node $parsed.Tree)
$matched = [bool](Test-IslFilterNode -Node $parsed.Tree -Facts $facts)
$applicable = if ($Mode -eq 'Include') { $matched } else { -not $matched }
$reason = if ($Mode -eq 'Include' -and $matched) {
'Included: the rule matches this device, so the assignment applies'
Expand Down
42 changes: 42 additions & 0 deletions Tests/Unit/Private/Compare-IslFilterVersion.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#Requires -Modules @{ ModuleName = 'Pester'; ModuleVersion = '6.2.0' }

<#
The order of two versions in an assignment filter.
#>

BeforeAll {
$script:ModuleRoot = Split-Path -Parent (Split-Path -Parent (Split-Path -Parent $PSScriptRoot))
if (-not (Get-Module -Name IntuneScriptLab)) {
Import-Module (Join-Path $script:ModuleRoot 'IntuneScriptLab.psd1')
}

function script:Compare-Pair {
param([string]$Actual, [string]$Expected)
InModuleScope IntuneScriptLab -Parameters @{ Actual = $Actual; Expected = $Expected } {
@(Compare-IslFilterVersion -Actual $Actual -Expected $Expected)
}
}
}

Describe 'Compare-IslFilterVersion' -Tag 'Unit', 'Private' {

It 'orders <Actual> against <Expected> as <Order>' -ForEach @(
@{ Actual = '10.0.26100'; Expected = '10.0.22631'; Order = 1 }
@{ Actual = '10.0.9'; Expected = '10.0.10'; Order = -1 }
@{ Actual = '10.0'; Expected = '10.0.0.0'; Order = 0 }
@{ Actual = '10.0.26100.4202'; Expected = '10.0.26100.4203'; Order = -1 }
) {
$result = Compare-Pair -Actual $Actual -Expected $Expected

$result | Should-BeCollection -Count 1
$result[0] | Should-Be $Order
}

It 'gives no order when <Case>' -ForEach @(
@{ Case = 'the device value is not a version'; Actual = 'unknown'; Expected = '10.0' }
@{ Case = 'the rule value is not a version'; Actual = '10.0'; Expected = 'Windows 11' }
@{ Case = 'the device has no value'; Actual = ''; Expected = '10.0' }
) {
Compare-Pair -Actual $Actual -Expected $Expected | Should-BeCollection @()
}
}
Loading