Private/Utils/New-PolicyIndex.ps1
|
function New-PolicyIndex { <# .SYNOPSIS Create hashtable index for fast policy lookup by normalized name. .DESCRIPTION Builds a hashtable where keys are normalized policy names and values are arrays of policy objects with that name. Enables O(1) lookup performance. .PARAMETER Items Array of policy objects to index. .PARAMETER NameProperty Property name containing the policy name (e.g., "DisplayName", "PolicyName"). .EXAMPLE $index = New-PolicyIndex -Items $policies -NameProperty "DisplayName" $matchingPolicies = $index["deploy vm backup"] .OUTPUTS Hashtable with normalized names as keys and policy arrays as values #> [CmdletBinding()] param( [Parameter(Mandatory = $true)] [object[]]$Items, [Parameter(Mandatory = $true)] [string]$NameProperty ) $hashtable = @{} foreach ($item in $Items) { $key = Normalize-PolicyName ($item.$NameProperty) if (-not $key) { continue } if (-not $hashtable.ContainsKey($key)) { $hashtable[$key] = @() } $hashtable[$key] += $item } return $hashtable } |