Wednesday, December 12, 2007

PowerShell Example

Here's a real quick Windows PowerShell example I just used.

I've been experimenting with .NET's Window's EventLog support. There's a class in System.Diagnositics called EventLog which lets you query the event log and add entries and so forth.

I had a C# program which created an event source on the Application Log like this:
if( !System.Diagnostics.EventLog.SourceExists("TestSource") )
System.Diagnostics.EventLog.CreateEventSource("TestSource",
"Application");

and I needed to change it so that the event source was on a new custom log like this:
if ( !System.Diagnostics.EventLog.SourceExists("TestSource") )
System.Diagnostics.EventLog.CreateEventSource("TestSource",
"CustomLog");

To do this, I needed to delete the old event source, otherwise I would get an error because I was trying to use the event source with the wrong log.

I was about to write a tiny little C# program to say:
System.Diagnostics.EventLog.DeleteEventSource("TestSource");

And then I thought, "PowerShell!"

PS C:\> [System.Diagnostics.EventLog]::DeleteEventSource(
"TestSource")

1 comment:

  1. The create event source works better with :
    [system.diagnostics.eventlog]::CreateEventSource("CustomSource", "Customlog")

    .. i guess!
    -Vaibhav Singh.

    ReplyDelete

Note: Only a member of this blog may post a comment.