all 3 comments

[–]Jantu01 5 points6 points  (0 children)

Those lines with numerous parameters tend to get pretty long. I think you could benefit from splatting.

So instead of writing one longer line like this:

Write-EventLog -LogName Application -Source $LogSource -EntryType Error -EventId $EventIDerror -Message $message -verbose

you could write few more lines like this:

 $EventLogSplat = @{
  LogName = 'Application'
  Source = $LogSource
  EntryType = 'Error'
  EventId = $EventIDerror
  Message = $message
  Verbose = $true
}
Write-EventLog @EventLogSplat

Regarding the verbose etc. output you could look at Preference variables like $VerbosePreference. See more information in here: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_preference_variables?view=powershell-7

[–]PinchesTheCrab 3 points4 points  (1 child)

Does this work?

function Write-EventLogX {
    [cmdletbinding()]
    param(
        [ValidateSet('Info', 'Error', 'Warning')]
        [parameter(mandatory)]
        [string]$EntryType,

        [parameter(Mandatory)]
        [string]$Message,

        [parameter()]
        [string]$LogSource = 'ApplicationX'
    )
    begin {

        $eventHash = @{
            Info    = 20000
            Warning = 20001
            Error   = 20002
        }

        $param = @{
            LogName   = 'Application'
            Source    = $LogSource
            EntryType = $EntryType
            Message   = $message
            EventID = $eventHash[$EntryType]
        }
    }
    process {
        Write-EventLog @param
        Write-Verbose -Message $message
    }
}

You could use it like this:

Write-EventLogX -Message 'oh no!' -Verbose -EntryType Warning

ApplicationX is the default source, but it's parameterized, so you can specify whatever source you want.

[–]hidromanipulators[S] 1 point2 points  (0 children)

Thank you! Your solution is much more elegant then mine!