Skip to content

Instantly share code, notes, and snippets.

@alexinnes
Last active October 26, 2016 08:58
Show Gist options
  • Save alexinnes/0b72b4e1ea0f6b554ae6bb4ac6c46cd6 to your computer and use it in GitHub Desktop.
Save alexinnes/0b72b4e1ea0f6b554ae6bb4ac6c46cd6 to your computer and use it in GitHub Desktop.
Create an EventLog and a LogFile.
function Create-ScriptEventLog
{
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$True,
Position=1)]
[string]$Source = "",
[Parameter(Mandatory=$false,
Position=0)]
[string]$LogName = "ScriptHealthCheck"
)
Begin
{
}
Process
{
if (!(Get-EventLog -list | where {$_.logdisplayname -eq $LogName})){
New-EventLog -Source $Source -LogName $LogName
}
else {
Write-Error "$LogName Log has already been created"
}
}
End
{
}
}
function Write-ScriptEventLog
{
[CmdletBinding(DefaultParameterSetName="Default")]
[Alias()]
Param
(
[Parameter(
Mandatory=$True,
ValueFromPipelineByPropertyName=$true,
ParameterSetName="Default",
Position=1
)]
[Parameter(
Mandatory=$True,
ValueFromPipelineByPropertyName=$true,
ParameterSetName="Non-Default",
Position=2
)]
[ValidateSet ("Info","Warning","Error")]
$ErrorType,
[Parameter(
Mandatory=$True,
ValueFromPipelineByPropertyName=$true,
ParameterSetName="Default",
Position=0
)]
[Parameter(
Mandatory=$True,
ValueFromPipelineByPropertyName=$true,
ParameterSetName="Non-Default",
Position=3
)]
[string]$Message,
[Parameter(
Mandatory=$True,
ValueFromPipelineByPropertyName=$true,
ParameterSetName="Default",
Position=2
)]
[Parameter(
Mandatory=$True,
ValueFromPipelineByPropertyName=$true,
ParameterSetName="Non-Default",
Position=5
)]
[int]$Category,
[Parameter(
Mandatory=$True,
ValueFromPipelineByPropertyName=$true,
ParameterSetName="Default",
Position=3
)]
[Parameter(
Mandatory=$True,
ValueFromPipelineByPropertyName=$true,
ParameterSetName="Non-Default",
Position=4
)]
[int]$EventID,
[Parameter(
Mandatory=$False,
ValueFromPipelineByPropertyName=$true,
ParameterSetName="Non-Default",
Position=0
)]
$LogName = "ScriptHealthCheck",
[Parameter(
Mandatory=$false,
ValueFromPipelineByPropertyName=$true,
ParameterSetName="Non-Default",
Position=1
)]
$Source = (Get-Variable -Scope 1 -Name MyInvocation).value.MyCommand.Name,
[Parameter(
Mandatory=$False,
ValueFromPipelineByPropertyName=$true,
ParameterSetName="Default"
)]
[Parameter(
Mandatory=$False,
ValueFromPipelineByPropertyName=$true,
ParameterSetName="Non-Default"
)]
[string]$LogFile = "C:\Script\HealthCheck.log"
)
Begin
{
$Date = Get-Date
}
Process
{
if ([System.Diagnostics.EventLog]::SourceExists($Source)){
Write-EventLog -LogName $logname -Source $Source -EntryType $ErrorType -Message $Message -Category $Category -EventId $EventID
}
else {
New-EventLog -LogName $logname -Source $Source
Write-EventLog -LogName $logname -Source $Source -EntryType $ErrorType -Message $Message -Category $Category -EventId $EventID
}
try{
Out-File $LogFile -NoClobber string -InputObject "$Date : $Source : $ErrorType : $Message" -Append
}
catch {
Write-Error "Could not write to the log file, please check the Log file: $LogFile"
}
}
End
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment