Improve Get-WinEvent -ListLog exception handling#27395
Merged
daxian-dbw merged 2 commits intoPowerShell:masterfrom May 5, 2026
Merged
Improve Get-WinEvent -ListLog exception handling#27395daxian-dbw merged 2 commits intoPowerShell:masterfrom
daxian-dbw merged 2 commits intoPowerShell:masterfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR improves Get-WinEvent -ListLog error handling by distinguishing access-denied failures from “log not found” scenarios and by avoiding catching PipelineStoppedException during output, improving UX and pipeline behavior.
Changes:
- Add dedicated handling for
UnauthorizedAccessExceptionto emit a permission-denied error with a recommended action. - Move
WriteObjectoutside the try/catch soSelect-Object -Firstcan stop the pipeline cleanly. - Add new localized resource strings for the new error message and recommendation text.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
src/Microsoft.PowerShell.Commands.Diagnostics/resources/GetEventResources.resx |
Adds new resource strings used for access-denied messaging and elevation recommendation. |
src/Microsoft.PowerShell.Commands.Diagnostics/GetEventCommand.cs |
Updates ProcessListLog() to catch access-denied separately and avoid catching pipeline-stop exceptions during output. |
| logInfoObj = eventLogSession.GetLogInformation(logName, PathType.LogName); | ||
| } | ||
| catch (UnauthorizedAccessException exc) | ||
| { |
Contributor
Author
There was a problem hiding this comment.
Certain log types are skipped so this cannot be moved to an earlier point.
Comment on lines
+258
to
+262
| <data name="LogInfoNoAccess" xml:space="preserve"> | ||
| <value>Access denied for log: '{0}'.</value> | ||
| </data> | ||
| <data name="SuggestElevation" xml:space="preserve"> | ||
| <value>Launch PowerShell with elevated user rights.</value> |
Contributor
Author
There was a problem hiding this comment.
But what is the text file used for?
Comment on lines
+574
to
+576
| outputObj.Properties.Add(new PSNoteProperty("RecordCount", logInfoObj.RecordCount)); | ||
|
|
||
| WriteObject(outputObj); |
Co-authored-by: Copilot Autofix powered by AI <175728472+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.
PR Summary
This PR improves the error handling for
Get-WinEvent -ListLogby adding a dedicated catch block for UnauthorizedAccessException exceptions and preventing an incorrect error message about not finding the specified log from showing up if the event log was found, but was inaccessible. See this before and after for the UXBefore:
After:
WriteObject was also moved outside of the try-catch to fix an issue where the
PipelineStoppedExceptionexception was being caught, which preventedSelect -First Xfrom working.I haven't added any tests because I don't see what I'd need to test to avoid regressions here.
PR Context
Fixes: #27390
PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright header