Avoid random error with Write-EventLog in Windows Server 2025#1473
Avoid random error with Write-EventLog in Windows Server 2025#1473Yvand wants to merge 1 commit intodsccommunity:masterfrom
Conversation
WalkthroughA bug fix suppresses ChangesEvent Log Error Suppression
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Tip 💬 Introducing Slack Agent: The best way for teams to turn conversations into code.Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.
Built for teams:
One agent for your entire SDLC. Right inside Slack. 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. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
SharePointDsc/Modules/SharePointDsc.Util/SharePointDsc.Util.psm1 (1)
48-56: ⚡ Quick winNon-terminating
Write-EventLogerrors are now silently discarded — consider routing them through the existingcatchfor at least a verbose trace.
try/catchonly catches terminating errors;-ErrorAction SilentlyContinueonly suppresses non-terminating errors. They handle disjoint error classes and the combination is correct for preventing deployment interruption.However, non-terminating errors (the most likely class for the WS2025 registry-key failure) are now silently dropped — they no longer appear in the verbose stream, in
$Error, or anywhere else. TheWrite-Verbose -Message $_in thecatchblock still fires only for terminating errors.Using
-ErrorAction Stopinstead converts everyWrite-EventLogerror into a terminating error, which the existingcatchthen captures and surfaces viaWrite-Verbose. Deployments still continue and the failure is at least observable under-Verbose.♻️ Proposed change
try { Write-EventLog -LogName $LogName -Source $Source ` - -EventId $EventID -Message $Message -EntryType $EntryType -ErrorAction SilentlyContinue + -EventId $EventID -Message $Message -EntryType $EntryType -ErrorAction Stop } catch { Write-Verbose -Message $_ }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@SharePointDsc/Modules/SharePointDsc.Util/SharePointDsc.Util.psm1` around lines 48 - 56, Change the Write-EventLog call to allow non-terminating errors to be handled by the existing catch: replace the -ErrorAction SilentlyContinue on the Write-EventLog invocation so that errors become terminating and are captured by the surrounding try/catch; then surface the caught error via the existing Write-Verbose $_ in the catch block so failures (like the WS2025 registry-key issue) are visible under -Verbose while still not aborting deployment. Reference: the Write-EventLog call and the surrounding try/catch with Write-Verbose.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@SharePointDsc/Modules/SharePointDsc.Util/SharePointDsc.Util.psm1`:
- Around line 48-56: Change the Write-EventLog call to allow non-terminating
errors to be handled by the existing catch: replace the -ErrorAction
SilentlyContinue on the Write-EventLog invocation so that errors become
terminating and are captured by the surrounding try/catch; then surface the
caught error via the existing Write-Verbose $_ in the catch block so failures
(like the WS2025 registry-key issue) are visible under -Verbose while still not
aborting deployment. Reference: the Write-EventLog call and the surrounding
try/catch with Write-Verbose.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 1423c6ab-2627-4988-be34-ebcd473279db
📒 Files selected for processing (2)
CHANGELOG.mdSharePointDsc/Modules/SharePointDsc.Util/SharePointDsc.Util.psm1
Pull Request (PR) description
Randomly (about 15-20%), deployments fail in Windows Server 2025 because of error "The registry key for the log "SPDsc" for source "MSFT_SPFarm" could not be opened", when running cmdlet
Write-EventLogin functionAdd-SPDscEvent.Please note:
This Pull Request (PR) fixes the following issues
Task list
Entry should say what was changed and how that affects users (if applicable), and
reference the issue being resolved (if applicable).
help.
This change is