Posts

Showing posts with the label EventViewer

Event Log Custom Sources

There are many times where we have to write events on the event log in order to log what a script or program is doing, like an automation scrip for example. We want to be able to find out if a script run or not and where the problem was, if any. I usually create a separate event source on the application log for that purpose, in order to be able to filter the events and not get lost in the application log. Before adding the logging functionality to your script. lets see a couple of commands that are going to be very helpful. First. lets see how we can create a new source. New-EventLog -LogName Application -Source "MyScript" This command will create the source named "MyScript" in the application log. Another command that may come in handy is: [System.Diagnostics.EventLog] :: SourceExists( "MyScript" ) This command will return true if there is a source named "MyScript" on the application log. I've been using this command w...

PowerShell Log

I'm starting to have many jobs that run powershell scripts on some servers so I decided to update the scripts in order to write to the event logs on the servers. I decided to create my own log in order to be free to do anything I want with it and I called it "Powershell Jobs". I also added a source for every job. All that with the New-EventLog command. I then added error handling code to the scripts using the $error.Count value in order to check if the last command had any errors and the $error[0] for the error message. I then write to the above log using the Write-EventLog command. That way I know what happened for every job and I can get it on Splunk (or any other syslog server) too. One more thing, depending on the result of a command I write error or information events and I'm using a seperate ID for every event type.