Monthly Archives: October 2011

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" 
    }
}
%d bloggers like this: