Skip to content

Get-WinEvent - Suggested enhancements #10637

Description

Steps to reproduce

Get-WinEvent

Expected behavior

A user friendly error message when running from a non-elevated session, stating that elevated permissions is needed.

I would suggest that the behavior of the default parameter set prompts for required parameters rather than returning cryptic error messages. For example, prompt for the -LogName parameter if no parameters is supplied.

Actual behavior

Get-WinEvent : Could not retrieve information about the Security log. Error: Attempted to perform an unauthorized operation..
At line:1 char:1
+ Get-WinEvent
+ ~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [Get-WinEvent], Exception
+ FullyQualifiedErrorId : LogInfoUnavailable,Microsoft.PowerShell.Commands.GetWinEventCommand

Get-WinEvent : The data is invalid
At line:1 char:1
+ Get-WinEvent
+ ~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [Get-WinEvent], EventLogInvalidDataException
+ FullyQualifiedErrorId : The data is invalid,Microsoft.PowerShell.Commands.GetWinEventCommand        

The "Get-WinEvent : The data is invalid" error is repeated many times, regardless of running elevated or not.

Environment data

Name                           Value
----                           -----
PSVersion                      6.2.3
PSEdition                      Core
GitCommitId                    6.2.3
OS                             Microsoft Windows 10.0.17763
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0                         

Activity

  1. added
    Issue-Question
    on Sep 27, 2019
  2. iSazonov commented on Sep 27, 2019

    @iSazonov
    Collaborator

    Really here is two issues.

    • Get-WinEvent bad error message if not elevated.
    • Get-WinEvent writes something to output even if error was (it is Get-WinEvent : The data is invalid case)
  3. added
    WG-Cmdlets
    on Sep 27, 2019
  4. janegilring commented on Sep 27, 2019

    @janegilring
    Author

    Ok, should I edit this issue to only include the first, and create a separate issue for the latter?

  5. iSazonov commented on Sep 27, 2019

    @iSazonov
    Collaborator

    Jan Egil Ring (@janegilring) You could open new issue for your suggestion

    I would suggest that the behavior of the default parameter set prompts for required parameters rather than returning cryptic error messages. For example, prompt for the -LogName parameter if no parameters is supplied.

  6. iSazonov commented on Sep 27, 2019

    @iSazonov
    Collaborator

    I pulled #10639 to reword the first error message.

  7. iSazonov commented on Sep 27, 2019

    @iSazonov
    Collaborator

    Second message "The data is invalid" comes from underlying API. Perhaps we generate a huge query. I found that ReadEvent() returns the error if our query contains number of logs more then 256.
    I think the design was ok for Windows XP with 3 logs, perhaps it worked on Windows 7 but it has already lost all meaning - now Windows 10 has over 400 logs and the query is no longer even executed.

    I did not find that 256 limit is documented. Nevertheless, we can verify that the filter returns more 256 log names and issue an appropriate message.

  8. added
    Issue-Bug
    and removed
    Issue-Question
    on Sep 30, 2019
  9. iSazonov commented on Sep 30, 2019

    @iSazonov
    Collaborator

    As for "prompt for the -LogName parameter if no parameters is supplied."

    Jan Egil Ring (@janegilring) Design is to get snapshot of all events from all logs for a time lot to analyze them. You could open new issue to discuss the design but I do not see the point.

  10. KevinMarquette commented on Oct 3, 2019

    @KevinMarquette
    Contributor

    I ran Get-WinEvent with no parameters and got 1665 errors saying the data is invalid.

  11. iSazonov commented on Oct 3, 2019

    @iSazonov
    Collaborator
  12. Robo210 commented on Nov 25, 2019

    @Robo210
    Contributor

    As for "prompt for the -LogName parameter if no parameters is supplied."

    Jan Egil Ring (@janegilring) Design is to get snapshot of all events from all logs for a time lot to analyze them. You could open new issue to discuss the design but I do not see the point.

    Ilya (@iSazonov) In case there is any interest the behavior again in the future, it would probably be best to simply check the event count for each channel and skip adding channels with 0 events to the query. This will cause new events that get logged to one of these empty channels during the query execution to get "missed", but if you consider the design to be to get all the events that were present at the exact moment the user ran the command, this should be fine.

    I did not find that 256 limit is documented.

    Neither could I, nor could I find any rationale for the limitation in the code. Perhaps a future version of Windows can remove this limit, though you'd still have to deal with older versions that error-out.

    now Windows 10 has over 400 logs and the query is no longer even executed.

    The query is actually running hundreds or thousands of times, which is why it prints so many error messages. In fact, I believe every single requested event is being retrieved from the Event Log service, and the error is being printed for every batch of 64 events being retrieved.

    private void ReadEvents(EventLogQuery logQuery)
    {
        using (EventLogReader readerObj = new EventLogReader(logQuery))
        {
            Int64 numEvents = 0;
            EventRecord evtObj = null;
    
            while (true)
            {
                try
                {
                    evtObj = readerObj.ReadEvent();
                }
                catch (Exception exc)
                {
                    WriteError(new ErrorRecord(exc, exc.Message, ErrorCategory.NotSpecified, null));
                    continue;
                }

    The error occurs in readerObj.ReadEvent(), and when the exception is thrown it just continues to the next event.

    The exception is getting thrown from the call to GetNextBatch within EventLogReader.ReadEvent. When it gets thrown this.currentIndex is not incremented, so the next call to ReadEvent causes it to retrieve a new batch of events, and so on until the service (which is independently tracking the current index) says there are no more events and GetNextBatch succeeds and sets this.eventCount to 0, causing ReadEvent to gracefully exit with this.isEof == true.

    public EventRecord ReadEvent(TimeSpan timeout) {
    
        EventLogPermissionHolder.GetEventLogPermission().Demand();
    
        if (this.isEof)
            throw new InvalidOperationException();
    
        if (this.currentIndex >= this.eventCount) {
            // buffer is empty, get next batch.
            GetNextBatch(timeout);
    
            if (this.currentIndex >= this.eventCount) {
                this.isEof = true;
                return null;
            }
        }
    
        EventLogRecord eventInstance = new EventLogRecord(new EventLogHandle(this.eventsBuffer[currentIndex], true), this.eventQuery.Session, this.cachedMetadataInformation);
        currentIndex++;
        return eventInstance;
    }
  13. iSazonov commented on Nov 25, 2019

    @iSazonov
    Collaborator

    Kyle Sabo (@Robo210) Thanks!
    As soon as we get feedback with a real scenario, we can improve something.

  14. iSazonov commented on Nov 25, 2019

    @iSazonov
    Collaborator

    Kyle Sabo (@Robo210) Thanks!
    As soon as we get feedback with a real scenario, we can improve something.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-BugResolution-FixedWG-Cmdlets

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions