I would like to retrieve information about event providers using Windows PowerShell? I'm running Windows 8.1 with PowerShell version 4.0, and I noticed that there are some .NET classes in the System.Diagnostics.Eventing
namespace that offer some functionality around Windows eventing.
I can create an EventProvider
instance by calling its default constructor, however this does not allow me to get any information about the event providers installed on the system.
$EventProvider = New-Object -TypeName System.Diagnostics.Eventing.EventProvider -ArgumentList ([System.Guid]'{00000000-0000-0000-0000-000000000000}');
How can I get more information about Event Tracing for Windows (ETW) providers that are installed on a system, and interact with Windows event logs, using Windows PowerShell?
I am already aware that I can use the command logman.exe query providers
, as described here, to retrieve the ETW provider list, and query the Windows event logs, but this is not very PowerShell friendly.
There is another .NET namespace called
System.Diagnostics.Eventing.Reader
, which contains a lot more .NET classes that allow you to retrieve information about Event Tracing for Windows (ETW) providers and event logs that are registered with a Windows operating system. Most of these types are defined in theSystem.Core.dll
.NET Assembly in the .NET Global Assembly Cache (GAC).For example, you can perform the following actions (and more):
ETW Provider Names
One of the core functions with ETW is getting a list of ETW providers are that are installed on a given system. You can easily retrieve this information with the .NET Framework types in the
System.Diagnostics.Eventing.Reader
namespace. There just so happens to be a .NET class namedEventLogSession
, and on this class is a static property namedGlobalSession
, which automatically retrieves a session/connection to the Event Log service on the local computer. If necessary, you can alternatively connect to a remote computer by using one of the constructors on theEventLogSession
class.Once you have retrieved an instance of the
EventLogSession
class, you can call theGetProviderNames()
method to retrieve a collection ofString
objects that represent the names of the installed ETW providers on the computer.Here is an example of retrieving the provider names from the local computer:
Here is an example of retrieving the ETW provider names from a remote computer using an alternate constructor:
You can use a different constructor of the
EventLogSession
class to specify alternate credentials to the remote computer. The alternate constructor for theEventLogSession
class requires the following parameters:SecureString
)System.Diagnostics.Eventing.Reader.SessionAuthentication
typeHere is an example of how to achieve that:
ETW Log Names
Once you have discovered all of the ETW providers installed on a computer, you may also wish to explore a complete list of the ETW logs that available on a computer. The
EventLogSession
class also has a method calledGetLogNames()
, which returns a collection ofString
objects that represent the ETW logs available on a target system. Similar to theGetProviderNames()
method, you can callGetLogNames()
on a local or remote computer.Here is an example of retrieving the ETW log names from the local computer:
Here is an example of retrieving the ETW log names from a remote computer:
ETW Provider Metadata
In addition to retrieving ETW provider names, you might also wish to retrieve more detailed information about them. You can do this using the
ProviderMetadata
class in theSystem.Diagnostics.Eventing.Reader
.NET class. Information provided by theProviderMetadata
class includes:Similar to the ETW provider and ETW log names, you can retrieve provider metadata from the local or remote system. In the latter case, you must establish an
EventLogSession
instance before you attempt to instantiate theProviderMetadata
class.Here is an example of retrieving ETW provider metadata from the local system:
To retrieve ETW provider metadata from a remote system, build your
EventLogSession
object before instantiating theProviderMetadata
class, and when you do instantiateProviderMetadata
, make sure you pass in the following parameters to the constructor:EventLogSession
instanceCultureInfo
object...
Note: You might get some exceptions when you instantiate
ProviderMetadata
objects through authenticated connections:Reading ETW Event Logs
Since you mentioned that you wanted to read events from ETW event logs also, this is easy to do with the types in the
System.Diagnostics.Eventing.Reader
.NET namespace as well. TheEventLogReader
class contains a method calledReadEvent()
which continually reads the next events from the event log specified when theEventLogReader
was instantiated.Here is a simple example of reading events from the System event log:
ETW Event Log Configuration
Similar to the provider metadata retrieved earlier, you can retrieve information about the configuration of a particular ETW event log. To do this, you instantiate the
EventLogConfiguration
class, passing in the name of an ETW event log. A variety of information about the event log will be returned, including:Here is an example of how to retrieve this information: