Open
Conversation
Replace seven identical Get-ADObject | Select-Object objectClass calls in Private\Set-RiskRating.ps1 with a new Get-SidObjectClass helper that wraps the query in a script-scoped hashtable cache (\). During a Locksmith scan, the same SID (Domain Users, Authenticated Users, Domain Computers, etc.) appears across many issues. Previously each issue evaluation issued a separate LDAP query for each SID. With the cache, every SID is resolved at most once per module session. Changes: - Add Get-SidObjectClass private helper at top of Set-RiskRating.ps1 - Replace all 7 Get-ADObject lookup sites with Get-SidObjectClass calls - Preserve exact return types and semantics for all call sites: - Pattern A (6 sites): returns PSCustomObject with objectClass property - Pattern B (1 site, ESC5): extracts .objectClass from the PSCustomObject - No changes to risk scoring logic, remediation, or test coverage Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR reduces repeated Active Directory lookups during a Locksmith scan by introducing a script-scoped SID→objectClass cache and routing existing Get-ADObject calls through a new helper in Set-RiskRating.
Changes:
- Added
Get-SidObjectClasshelper that cachesGet-ADObjectSID lookups in a script-scoped hashtable. - Replaced 7 repeated
Get-ADObject ... | Select-Object objectClasscall sites withGet-SidObjectClassto reuse cached results.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Use -ErrorAction Stop with try/catch so that a transient ADWS error does not populate the cache with . Legitimate 'not found' (SID absent from AD) still returns and caches normally; transient errors emit Write-Warning and leave the key absent, allowing retry on the next call. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Eliminates repeated Active Directory LDAP queries for the same SID during a Locksmith scan run by introducing a session-scoped lookup cache in
Private/Set-RiskRating.ps1.Problem
Set-RiskRatingis called once per issue during a scan. Seven call sites within that function all perform an identical query:Common principals — Domain Users (
S-1-5-21-...-513), Authenticated Users (S-1-5-11), Domain Computers (S-1-5-21-...-515), etc. — appear in the ACL of many templates and are re-queried on every issue that references them.Solution
New helper:
Get-SidObjectClassAdded at the top of
Set-RiskRating.ps1(beforeSet-RiskRating), automatically dot-sourced byLocksmith.psm1with the rest ofPrivate/*.ps1:The
$script:scope means the cache persists across all calls toSet-RiskRatingwithin a single module session, hitting AD at most once per unique SID per scan run.Call-site replacements (7 total)
All seven
Get-ADObject | Select-Object objectClasscalls replaced withGet-SidObjectClass:$IdentityReferenceObjectClass$IdentityReferenceObjectClass$escIdentityReferenceObjectClass$escIdentityReferenceObjectClass$escIdentityReferenceObjectClass$escIdentityReferenceObjectClass.objectClass$OtherIssueIdentityReferenceObjectClassPattern A preserves the PSCustomObject return type. Pattern B (ESC5) appends
.objectClassto extract the raw array, preserving exact existing semantics.Scope
Set-RiskRating.ps1Invoke-Locksmith.ps1not edited — it is auto-generated byBuild/Build-Module.ps1Static analysis
PSScriptAnalyzer (Warning+Error): no new warnings introduced. The pre-existing
PSUseShouldProcessForStateChangingFunctionsonSet-RiskRatingis unchanged.