Powershell script to create event source

Writing entries to application event log is a fairly common routine, and one of the thing to note is that the event source might not already exist and the user account under which your code runs might not have the necessary permissions to create them, which is completely acceptable and in fact, recommended.

Thus, a common practice is to create the necessary event sources upfront during deployment. This can be done using the folloing powershell script:

$eventSources = @(
"Department.Solution.Common",
"Department.Solution.Common.SPHelper",
"Department.Solution.FeatureReceivers",
"Department.Solution.WebControls",
"Department.Solution.WebParts",
"Department.Solution.EventReceivers",
"Department.Solution.Services"
)

foreach($source in $eventSources) {
    write-host "Creating event source $source"
    if ([System.Diagnostics.EventLog]::SourceExists($source) -eq $false) {
        [System.Diagnostics.EventLog]::CreateEventSource($source, "Application")
		write-host -foregroundcolor green "Event source $source created" 
    }
    else
    {
        write-host -foregroundcolor yellow "Warning: Event source $source already exists" 
    }
}
Advertisements

Posted on October 14, 2011, in Scripting and tagged , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: