Skip to content

Instantly share code, notes, and snippets.

@peaeater
Created June 19, 2014 00:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peaeater/c3f0bd2357af8667bb50 to your computer and use it in GitHub Desktop.
Save peaeater/c3f0bd2357af8667bb50 to your computer and use it in GitHub Desktop.
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