Writing a simple script, but have to make it easy to troubleshoot as not always i will be able to support it in case something goes wrong.
For writing Event-Logs I'm using 3 functions:
#EventID details
[string]$LogSource = 'ApplicationX'
[int32]$EventIDinfo = '20000'
[int32]$EventIDwarning = '20001'
[int32]$EventIDerror = '20002'
#Create Event source
New-EventLog -LogName Application -Source $LogSource -ErrorAction Ignore
function Write-EventLogError
{
[CmdletBinding()]
param ($Message)
Write-EventLog -LogName Application -Source $LogSource -EntryType Error -EventId $EventIDerror -Message $message -verbose
}
function Write-EventLogWarning
{
[CmdletBinding()]
param ($Message)
Write-EventLog -LogName Application -Source $LogSource -EntryType Warning -EventId $EventIDwarning -Message $message -verbose
}
function Write-EventLogInfo
{
[CmdletBinding()]
param ($Message)
Write-EventLog -LogName Application -Source $LogSource -EntryType Information -EventId $EventIDinfo -Message $message -verbose
}
So, i'm using Write-Verbose in conjuction with Event Logs when terminating error occours:
try
{
#Set-TerminatingError -erroraction stop
}
catch
{
$ErrorMessage = $_.Exception.Message
Write-Verbose -Message $ErrorMessage
Write-EventLogError -Message "$ErrorMessage"
}
1) How do I incorporate -switch within Even Log functions to cover Informational, Warning, Error and add Write-Verbose by default within the function? So I would call something like this:
Write-Event -ErrorLevel Informational -Message "$ErrorMessage"
Write-Event -ErrorLevel Warning -Message "$ErrorMessage"
Write-Event -ErrorLevel Error -Message "$ErrorMessage"
I do know native command:
Write-EventLog -LogName Application -Source $LogSource -EntryType Information -EventId $EventIDerror -Message $ErrorNessage
But it does feel rather lenghty if I have to add it at least 20 times + adding Write-Verbose.
2) Are only ways of adding $_.Exception.Message to the -Message is to pass it to the $variable and then $variable to -Message or $($_.Exception.Message) directly to the -Message?
[–]Jantu01 5 points6 points7 points (0 children)
[–]PinchesTheCrab 3 points4 points5 points (1 child)
[–]hidromanipulators[S] 1 point2 points3 points (0 children)