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"
}
}
Posted on October 14, 2011, in Scripting and tagged event log, Powershell. Bookmark the permalink. Leave a Comment.
Leave a Comment
Comments (0)