Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Adds supplied source value to the Windows Application Event Log. Requires admin privileges and will warn the user if elevation is required.
<#
Add log source to Application Event Log if not already there - REQUIRES ADMIN privileges
Peter Tyrrell
#>
param(
[Parameter(Mandatory=$false,ValueFromPipeline=$true,Position=0)]
[string]$logsrc = "Andi Solr Update"
)
# check for admin
if (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
Write-Warning ("You do not have admin privileges. Re-run this script as an administrator.")
break
}
try {
if (-not [System.Diagnostics.EventLog]::SourceExists($logsrc)) {
[System.Diagnostics.EventLog]::CreateEventSource($logsrc, "Application")
Write-Host ("INFO Source '{0}' added to Application Event Log." -f $logsrc)
}
else {
Write-Host ("INFO {0} is already a source in Application Event Log." -f $logsrc)
}
}
catch {
throw
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment