Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views :
TheServerGeeks

IT with everything IT

[How To]Use Event Logs in PowerShell Scripts

/

In This article, i will help you in adding the logging functionality to your PowerShell Script and reduce the errors that you may get along.

If you are not sure on how to use PowerShell to generated logs, I would strongly recommend you to read the below article.
[How To] Create EventLog using PowerShell 

Let’s assume that you have a script which can ping multiple servers and return the output. If you want to know more on this, Read the below article
[How To] use Test-Connection 

You will have to notify system administrators who manage the server from which you are running the script, that you are using PowerShell to test connections between multiple systems in the background. If the PowerShell is consuming more memory, This could help administrators to determine why. This could also help them in performance auditing or even while doing maintenance this could be a notification for them to wait for the process to finish.

I assume that you know how to use New-EventLog and Write-EventLog cmdlets by now. I will Just use the simple script to test connection and try to write event logs when it is started and ended. You can use the same logic/syntax in scripts and implement it as per your need.

I am using the below script to test the connection between few servers and output the results. 

$Computers = $env:COMPUTERNAME,’Server2′,’Client1′,’Client2′

foreach($comp in $computers){
Test-Connection -ComputerName $comp -Count 1 -Quiet -ErrorAction SilentlyContinue

}

Output of the above script will be similar to below. I know  this is very simple script, but when the list of input computers are increased to hundreds or thousands, you will need to log the process status.

So, Let’s jump right in and add the Event Log Support on this script. 

Whenever we write an event log, you need to have an appropriate Log Source for it. and when you run your script, Source should be present on the system, else it will throw an error. you can check for the source in the PowerShell script using .Net method. and the Final script will be similar to below. you can use IF and ElseIF functionalities on your script based on the requirement.

if (([System.Diagnostics.EventLog]::SourceExists(“PingTest”)) -ne ‘True’) {
New-EventLog -LogName system -Source ‘PingTest’
}

Write-EventLog -LogName System -Source ‘PingTest’ -EventId 2024 -Message “Starting to run Ping Test script Ping_test.ps1”

$Computers = $env:COMPUTERNAME,’Server2′,’Client1′,’Client2′

foreach($comp in $computers){
Test-Connection -ComputerName $comp -Count 1 -Quiet -ErrorAction SilentlyContinue
}

Write-EventLog -LogName System -Source ‘PingTest’ -EventId 2025 -Message “Ping Test script has completed”

Above Script is First checking for the Source, If the source, exists in the system, it is just ignores the create new source operation and continue to create event log, else, it will create the Source first and then write the event log.

I have added two logs in this script just to show you that you can generate multiple logs at any point in the scripts.

Comment Below and let me know how you implemented this functionality in your script or want to.

Leave a Comment

Your email address will not be published. Required fields are marked *

This div height required for enabling the sticky sidebar