Search code examples
powershellevent-log

PowerShell version 1.0 - Write to the event log


We have a client with PowerShell 1.0. I know in PowerShell 2.0, I have the Write-EventLog commandlet. However, this doesn't appear in PowerShell 1.0. I want my script to log events to the event log, mainly for debugging purposes.

How can I do this with PowerShell 1.0:? And, no I can't upgrade this computer to PowerShell 2.0. This is a customer production machine, and they don't want to touch the software on it.


Solution

  • You can use the .NET EventLog class to do this. It is pretty simple to use e.g.:

    $eventSource = "MyAppName"
    if (![Diagnostics.EventLog]::SourceExists($eventSource))
    {
        [Diagnostics.EventLog]::CreateEventSource($eventSource, "Application")
    }
    
    [Diagnostics.EventLog]::WriteEntry($eventSource, "message", [Diagnostics.EventLogEntryType]::Error)