Skip to content

refactor: explicit switch for CIM method names in Invoke-ComputerRegistry (STRUCT-5)#12

Open
dutch2005 wants to merge 1 commit into
masterfrom
refactor/struct-5-explicit-cim-method-switch
Open

refactor: explicit switch for CIM method names in Invoke-ComputerRegistry (STRUCT-5)#12
dutch2005 wants to merge 1 commit into
masterfrom
refactor/struct-5-explicit-cim-method-switch

Conversation

@dutch2005
Copy link
Copy Markdown
Owner

Summary

  • The Set branch of Invoke-ComputerRegistry composed the StdRegProv method name at runtime: \$methodName = \"Set\$(\$ValueType)Value\"
  • Runtime string composition hid the actual method target from reviewers and static analyzers
  • Replaced the if/elseif/elseif/else chain with a single explicit switch — each arm sets \$methodName as a literal and assigns the correctly-named \$cimArgs

Why

  • Auditability — a reviewer can grep SetStringValue, SetDWORDValue, SetQWORDValue and see every call site. With runtime composition, the string SetDWORDValue literally does not appear in the source
  • Drift resistance — if the ValidateSet attribute ever gains a new value ("MultiString", "Binary") without a matching method arm, the new code path now falls into the default branch (explicit [!] not yet implemented return) instead of attempting a CIM call to a method that may or may not exist by that composed name
  • Single-edit extension — adding a new supported type is one contiguous case-arm edit (method + args together), instead of two separate edits to the if-chain and the implicit-composition logic

Changes

  • LazyWinAdminModule/Private/Invoke-ComputerRegistry.ps1 — replaced the Set-branch composition + if-chain with an explicit switch. No change to Get / New / Remove branches, no parameter change, no return-value change.

Behavioral equivalence

ValueType Before (implicit) After (explicit)
String Set\ + String + Value = SetStringValue literal SetStringValue
DWord Set\ + DWord + Value = SetDWordValue ⚠️ literal SetDWORDValue
QWord Set\ + QWord + Value = SetQWordValue ⚠️ literal SetQWORDValue

⚠️ Caveat worth flagging to reviewers: the StdRegProv method names are SetDWORDValue / SetQWORDValue (all-caps DWORD/QWORD), but the ValueType ValidateSet uses PowerShell-idiomatic DWord / QWord. The previous composition produced SetDWordValue / SetQWordValue, relying on CIM's method-resolution being case-insensitive. The explicit switch now spells the method names the way StdRegProv documents them. Please confirm no regression on any caller that was depending on the case-insensitive match surviving.

Test plan

  • Set-Item via Invoke-ComputerRegistry -Action Set -ValueType DWord still writes the value on a local CIM session
  • Set-ComputerRDP (which touches fDenyTSConnections via DWORD set) still toggles RDP correctly
  • Passing an unsupported ValueType returns \$false with the existing warning text (no change)

Risks / rollback

  • Risk: The method-name case change (SetDWordValueSetDWORDValue) should be a no-op since CIM method resolution is case-insensitive, but any caller running under a CIM provider that enforces exact-case method names would newly succeed / newly fail based on the switch. Please verify in an integration run.
  • Rollback: git revert — the entire change is contained in one function, one branch of its switch.

Refs: code-review-2026-04-24.md STRUCT-5


@gemini-code-assist please review
@coderabbitai review
@Kilo please review

…stry (STRUCT-5)

The Set branch composed the StdRegProv method name at runtime from the
ValueType parameter: `$methodName = "Set$($ValueType)Value"`. That made
the actual method target depend on a runtime string, meaning:

  * A reviewer had to mentally concatenate three strings to know which
    method is being invoked.
  * PSScriptAnalyzer could not flag a drift between the ValidateSet
    attribute and the actual set of methods StdRegProv exposes.
  * Adding a new ValueType required updating three places (ValidateSet,
    the if/elseif chain for $cimArgs, and mentally verifying the method
    name composition still produced a real method).

Fix: replace the `if/elseif/elseif/else` with a single `switch` keyed
on $ValueType, where each arm sets both $methodName and $cimArgs
explicitly. Adding a new type is now a single contiguous edit; the
method target is a literal string that grep and static analyzers can
follow.

No behavioral change — the three supported types still map to the
same StdRegProv methods with the same argument shapes.

Refs: code-review-2026-04-24.md STRUCT-5
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 24, 2026

Warning

Rate limit exceeded

@dutch2005 has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 38 minutes and 52 seconds before requesting another review.

Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 38 minutes and 52 seconds.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: d9fd9398-c9a1-4b81-9e89-f4078adcd0a8

📥 Commits

Reviewing files that changed from the base of the PR and between 2f4ed7b and 10d29f8.

📒 Files selected for processing (1)
  • LazyWinAdminModule/Private/Invoke-ComputerRegistry.ps1
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch refactor/struct-5-explicit-cim-method-switch

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@dutch2005 dutch2005 added the refactor Code restructuring without behavior change label Apr 24, 2026
@kilo-code-bot
Copy link
Copy Markdown

kilo-code-bot Bot commented Apr 24, 2026

Code Review Summary

Status: No Issues Found | Recommendation: Merge

Files Reviewed (1 files)
  • LazyWinAdminModule/Private/Invoke-ComputerRegistry.ps1

Reviewed by nemotron-3-super-120b-a12b-20230311:free · 132,093 tokens

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the registry value setting logic in Invoke-ComputerRegistry.ps1 by replacing dynamic method name composition with an explicit switch statement, improving code auditability and maintainability. I have reviewed the changes and suggest implementing support for the 'ExpandString' registry type within the new switch block, as it is currently permitted by the parameter validation but not yet handled.

Comment on lines +72 to 89
switch ($ValueType) {
"String" {
$methodName = "SetStringValue"
$cimArgs = @{ hDefKey = $hDef; sSubKeyName = $KeyPath; sValueName = $ValueName; sValue = [string]$Value }
}
"DWord" {
$methodName = "SetDWORDValue"
$cimArgs = @{ hDefKey = $hDef; sSubKeyName = $KeyPath; sValueName = $ValueName; uValue = [uint32]$Value }
}
"QWord" {
$methodName = "SetQWORDValue"
$cimArgs = @{ hDefKey = $hDef; sSubKeyName = $KeyPath; sValueName = $ValueName; uValue = [uint64]$Value }
}
default {
Write-Warning "ValueType '$ValueType' is not yet fully implemented."
return $false
}
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The switch statement is a significant improvement for auditability. However, the ValidateSet for the $ValueType parameter (line 29) includes ExpandString, Binary, and MultiString. These types currently fall into the default block, which returns $false. While this is functionally equivalent to the previous implementation (which also didn't handle them in the if chain), it would be beneficial to implement ExpandString using the SetExpandedStringValue method, as it is a common registry type and already allowed by the parameter validation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

refactor Code restructuring without behavior change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant