Skip to content

Instantly share code, notes, and snippets.

@DanSmith
Created September 7, 2012 19:25
Show Gist options
  • Save DanSmith/3668859 to your computer and use it in GitHub Desktop.
Save DanSmith/3668859 to your computer and use it in GitHub Desktop.
Powershell script to register a windows event log source
# https://github.com/dansmith
#
$source = "My Application Name"
$wid=[System.Security.Principal.WindowsIdentity]::GetCurrent()
$prp=new-object System.Security.Principal.WindowsPrincipal($wid)
$adm=[System.Security.Principal.WindowsBuiltInRole]::Administrator
$IsAdmin=$prp.IsInRole($adm)
if($IsAdmin -eq $false)
{
[System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)
[Windows.Forms.MessageBox]::Show(“Please run this as an Administrator”,
“Not Administrator”,
[Windows.Forms.MessageBoxButtons]::OK,
[Windows.Forms.MessageBoxIcon]::Information)
exit
}
if ([System.Diagnostics.EventLog]::SourceExists($source) -eq $false)
{
[System.Diagnostics.EventLog]::CreateEventSource($source, "Application")
[System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)
[Windows.Forms.MessageBox]::Show(“Event log created successfully”,
“Complete”,
[Windows.Forms.MessageBoxButtons]::OK,
[Windows.Forms.MessageBoxIcon]::Information)
}
else
{
[System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)
[Windows.Forms.MessageBox]::Show(“Event log already exists”,
“Complete”,
[Windows.Forms.MessageBoxButtons]::OK,
[Windows.Forms.MessageBoxIcon]::Information)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment