Hello,
I am in the process of building a script that will be writing entries to the Event Logs. I am in the process of building this function:
function log-events {
Param (
[Parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,HelpMessage=“The message that will be logged in the servers event logs”)]
[string]$eventMessage,
[Parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,HelpMessage=“The severity of the event log; Information, Warning or Error”)]
[validateSet(“Information”,“Warning”,“Error”)]
[string]$loggingLevel,
[Parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,HelpMessage=“The ID that is associated with the event”)]
[string]$eventID
)
$EventLog = New-Object -type System.Diagnostics.Eventlog -argumentlist Application
$EventLog.Source = “Test Event Logs”
$EventLog.WriteEntry($eventMessage,$loggingLevel,$eventID)
}
This means that every time the function is called it will create a System.Diganostics.Eventlog instance. My question is, would I be better setting something like this:
$Global:EventLog = New-Object -type System.Diagnostics.Eventlog -argumentlist Application
and then just calling $EventLog from within the function. Is this just a different way to skin a cat or is there an advantage to using one method over the other?
Thanks
Tim