Skip to content

fix: replace Get-WmiObject with Get-CimInstance in UWF query (RULE-1)#5

Open
dutch2005 wants to merge 1 commit into
masterfrom
fix/rule-1-cim-replace-wmi
Open

fix: replace Get-WmiObject with Get-CimInstance in UWF query (RULE-1)#5
dutch2005 wants to merge 1 commit into
masterfrom
fix/rule-1-cim-replace-wmi

Conversation

@dutch2005
Copy link
Copy Markdown
Owner

Summary

Get-WmiObject was removed from PowerShell 7. The cmdlet does not exist in the 7.4+ runtime this module targets, so the Unified Write Filter branch of Get-DeviceComplianceStatus would throw CommandNotFoundException the moment an Enterprise SKU reached that code path. Swaps the single offending line for Get-CimInstance.

Changes

  • LazyWinAdminModule/Private/Get-DeviceComplianceStatus.ps1:86Get-WmiObject -Namespace ... -Class ...Get-CimInstance -Namespace ... -ClassName ....

Verified no other Get-WmiObject call sites exist in LazyWinAdminModule/Private/.

Test plan

  • LazyWinAdminModule/Tests/Run-Tests.ps1 — full suite passes on windows-latest
  • PSScriptAnalyzer clean on the modified file
  • Manual smoke on Windows Enterprise: Device Compliance tab still reports UWF status correctly

Risks / rollback

Low risk. Get-CimInstance against the UWF_Filter class is API-compatible for the one property read (CurrentEnabled). Rollback: git revert.

.speq compliance

  • CONTRACT CIM-ONLY — satisfied (no remaining Get-WmiObject in Private/).
  • VOCABULARY — uses CimSession conventions (none needed here — no session, one-shot call).
  • CALLS — CORE→nothing cross-layer; unchanged.

AI review

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

Related

`Get-WmiObject` was removed from PowerShell 7. The cmdlet does not exist in
the 7.4+ runtime this module targets, so the Unified Write Filter branch of
Get-DeviceComplianceStatus would throw `CommandNotFoundException` the moment
an Enterprise SKU reached that code path.

CIM is the supported replacement: same namespace, same class, same behavior.
Parameter name changes from `-Class` to `-ClassName`. No other call sites
use `Get-WmiObject` anywhere in Private/.

This is also explicitly forbidden by lazywinadmin.speq CONTRACT CIM-ONLY and
the Gemini / CodeRabbit style guides.

Ref: code-review-2026-04-24.md — RULE-1 (CRITICAL).
@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 57 minutes and 31 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 57 minutes and 31 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: c76612a1-63d4-46b6-b5b6-48b9948a44af

📥 Commits

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

📒 Files selected for processing (1)
  • LazyWinAdminModule/Private/Get-DeviceComplianceStatus.ps1
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/rule-1-cim-replace-wmi

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 bug Something isn't working 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/Get-DeviceComplianceStatus.ps1

Reviewed by nemotron-3-super-120b-a12b-20230311:free · 116,768 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 updates the Get-DeviceComplianceStatus script by replacing the deprecated Get-WmiObject cmdlet with Get-CimInstance. A review comment points out that if the CIM query returns no results, subsequent property access on the null variable will trigger a PropertyNotFoundException due to the module's strict mode settings. It is recommended to use the null-coalescing operator to provide a default object and prevent script failure.

$feature = Get-WindowsOptionalFeature -Online -FeatureName 'Client-UnifiedWriteFilter' -ErrorAction SilentlyContinue
if ($feature.State -eq 'Enabled') {
$uwf = Get-WmiObject -Namespace 'root\standardcimv2\embedded' -Class 'UWF_Filter' -ErrorAction SilentlyContinue
$uwf = Get-CimInstance -Namespace 'root\standardcimv2\embedded' -ClassName 'UWF_Filter' -ErrorAction SilentlyContinue
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

Since the module enables Set-StrictMode -Version Latest (configured in LazyWinAdminModule.psm1), accessing properties on a null variable will throw a PropertyNotFoundException. If Get-CimInstance returns no instances (which can occur if the UWF filter is not yet initialized or in certain transient error states), $uwf will be $null, causing the script to fail on lines 87 and 88.

Since this module targets PowerShell 7.4, you can use the null-coalescing operator (??) to provide a safe default object. This ensures that $status and $detail are populated correctly even if the WMI query returns no results, avoiding an unnecessary exception that would otherwise be caught by the generic try/catch block.

                $uwf    = (Get-CimInstance -Namespace 'root\standardcimv2\embedded' -ClassName 'UWF_Filter' -ErrorAction SilentlyContinue | Select-Object -First 1) ?? [PSCustomObject]@{ CurrentEnabled = $false }

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

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant