Skip to content

Avoid random error with Write-EventLog in Windows Server 2025#1473

Open
Yvand wants to merge 1 commit intodsccommunity:masterfrom
Yvand:avoid-random-error-in-Write-EventLog
Open

Avoid random error with Write-EventLog in Windows Server 2025#1473
Yvand wants to merge 1 commit intodsccommunity:masterfrom
Yvand:avoid-random-error-in-Write-EventLog

Conversation

@Yvand
Copy link
Copy Markdown
Contributor

@Yvand Yvand commented May 5, 2026

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-EventLog in function Add-SPDscEvent.

Please note:

  • This error is not easy to repro, it works fine most of the time, but somehow it happens from time to time with Windows Server 2025
  • This change is totally safe, since it only complements the existing try/catch (which does not trap cmdlet errors). Beside, if the logging fails, we really do not want this to interrupt the deployment.

This Pull Request (PR) fixes the following issues

Task list

  • Added an entry to the change log under the Unreleased section of the file CHANGELOG.md.
    Entry should say what was changed and how that affects users (if applicable), and
    reference the issue being resolved (if applicable).
  • Resource documentation added/updated in README.md.
  • Resource parameter descriptions added/updated in README.md, schema.mof and comment-based
    help.
  • Comment-based help added/updated.
  • Localization strings added/updated in all localization files as appropriate.
  • Examples appropriately added/updated.
  • Unit tests added/updated. See DSC Community Testing Guidelines.
  • Integration tests added/updated (where possible). See DSC Community Testing Guidelines.
  • New/changed code adheres to DSC Community Style Guidelines.

This change is Reviewable

@Yvand Yvand requested a review from ykuijs as a code owner May 5, 2026 11:08
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 5, 2026

Walkthrough

A bug fix suppresses Write-EventLog errors in the Add-SPDscEvent function to prevent random registry-key access failures on Windows Server 2025. The changelog documents this fix alongside an existing reboot-trigger correction.

Changes

Event Log Error Suppression

Layer / File(s) Summary
Core Implementation
SharePointDsc/Modules/SharePointDsc.Util/SharePointDsc.Util.psm1
Add-SPDscEvent adds -ErrorAction SilentlyContinue to the Write-EventLog call to suppress registry-key access errors on Windows Server 2025.
Documentation
CHANGELOG.md
Unreleased "Fixed" section documents the Write-EventLog registry-key error prevention in Add-SPDscEvent.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main change: suppressing a random Write-EventLog error in Windows Server 2025, which directly matches the pull request's core objective.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description check ✅ Passed The PR description clearly relates to the changeset, detailing a Windows Server 2025 issue with Write-EventLog errors and explaining the fix applied to the Add-SPDscEvent function.

✏️ 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.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


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.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
SharePointDsc/Modules/SharePointDsc.Util/SharePointDsc.Util.psm1 (1)

48-56: ⚡ Quick win

Non-terminating Write-EventLog errors are now silently discarded — consider routing them through the existing catch for at least a verbose trace.

try/catch only catches terminating errors; -ErrorAction SilentlyContinue only 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. The Write-Verbose -Message $_ in the catch block still fires only for terminating errors.

Using -ErrorAction Stop instead converts every Write-EventLog error into a terminating error, which the existing catch then captures and surfaces via Write-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

📥 Commits

Reviewing files that changed from the base of the PR and between 9d2f125 and ad235a7.

📒 Files selected for processing (2)
  • CHANGELOG.md
  • SharePointDsc/Modules/SharePointDsc.Util/SharePointDsc.Util.psm1

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant