Repository navigation
refactor(helpers): move the assignment filter's evaluator out #102
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: refactor/helpers-logs-graph
Are you sure you want to change the base?
Changes from all commits
f5eff17
edd31c5
c092570
646450c
75d5a5e
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,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 | ||
| } |
| 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) } | ||
|
Collaborator
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. Pre-existing (not from this change, not blocking): a version part too large for |
||
| while ($parts.Count -lt 4) { $parts.Add(0) } | ||
| $parts.ToArray() | ||
| } | ||
| 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 | ||
|
Collaborator
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. 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 |
||
| ) | ||
|
|
||
| $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 | ||
| } | ||
| 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 | ||
| } | ||
| } | ||
| } |
| 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 @() | ||
| } | ||
| } |
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.
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.