PowerShell 2.0: One Cmdlet at a Time 65 New-EventLog
Continuing the series looking at new cmdlets available in PowerShell 2.0. This time we look at the New-EventLog cmdlet.
What can I do with it?
Create a custom Event Log.
Example:
Create a custom Event Log named App1 with an event source of AppEvent. Use the Get-EventLog cmdlet to confirm it has been created. Tip: New-EventLog requires a PowerShell session with elevated privileges.
New-EventLog -LogName App1 -Source AppEvent Get-EventLog -List
You can see that the App1 Event Log has been created.
You can create entries in this log using the Write-EventLog cmdlet, e.g.
Write-EventLog -LogName App1 -Source AppEvent -ID 1020 -Message “Error 1020 has occured”
Here’s confirmation in Event Viewer that the App1 Event Log exists and we have created the above entry in it.
How could I have done this in PowerShell 1.0?
You could have used the .NET System.Diagnostics.EventLog class and the CreateEventSource method to create a custom event log.
$LogDetails = New-Object System.Diagnostics.EventSourceCreationData “AppEvent”, “App1” [System.Diagnostics.EventLog]::CreateEventSource($LogDetails)