- <#
- .SYNOPSIS
- Creates a new Event Log
- .DESCRIPTION
- This script first checks to see if a log called NewPSHLog exists. if not, it creates
- it then exits. if the log exists, the script writes an entry ot the log and then
- displays the log entries. A separate script clears and deletes the log!
- .NOTES
- File Name : New-CustomEventLog.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2 CTP3
- .LINK
- Script posted to:
- http://www.pshscripts.blogspot.com
- .EXAMPLE
- First time the script runs:
- PSH [C:\foo]: .\New-CustomEventLog.ps1'
- CreatedEventSource
- Exiting, execute the script a second time to use the source.
- Second time the script runs:
- PSH [C:\foo]: .\New-CustomEventLog.ps1'
- PowerShell Eventlog exists
- Index Time EntryType Source InstanceID Message
- ----- ---- --------- ------ ---------- -------
- 12 Feb 22 16:11 Information NewPshLog 0 Writing to new PowerShell even...
- #>
- ###
- # Start of Script
- ###
- if (![system.diagnostics.eventlog]::SourceExists("NewPSHLog")) {
- # An event log source should not be created and immediately used.
- # There is a latency time to enable the source, it should be created
- # prior to executing the application that uses the source.
- # So let's execute this sample a second time to use the new source.
- [system.diagnostics.EventLog]::CreateEventSource("MySource", "NewPSHLog")
- "CreatedEventSource"
- "Exiting, execute the script a second time to use the source."
- # The source is created. Exit the application to allow it to be registered.
- return
- }
- else {
- "NewPSHLog Eventlog exists"
- }
- # With log created, create an EventLog instance and assign its source.
- $mylog = new-object System.diagnostics.Eventlog
- $myLog.Source = "NewPshLog"
- # Write an informational entry to the event log.
- $myLog.WriteEntry("Writing to new PowerShell event log.")
- # Display log events
- get-eventlog "NewPSHLog"
- # End of Script
This blog contains PowerShell scripts, more PowerShell scripts and still more PowerShell scripts. Occasionally you may see some organisational posts.
Monday 23 February 2009
New-CustomEventLog.ps1
Subscribe to:
Post Comments (Atom)
3 comments:
Excellent! I googled around for a bit before finding this page that answered every question I had (and more)!
Thanks for posting this -- I've added your blog as a bookmark for future reference.
Shane O.
Glad it helped - sorry the Google results were't easier for you!
Suggestions for other scripts, most welcome.
Nice post, thanks. The New-EventLog command does this nowadays, however it is not properly documented anywhere how to use it and this post helped.
Post a Comment