Get SQL Server Logs with Powershell


In my last posts I talked about retrieving the Event Logs of Windows with Powershell. I had to do the same thing for the SQL Server logs with the same output.

The script works with three parameters:

  1. ServerList
  2. Output
  3. Rows

1. ServerList

This a mandatory parameter and has to point to a comma-seperated file with the following format:

2. Output

This is the output directory for the results. The parameter is not mandatory but it’s very usefull when you have many servers.

3. Rows

This is the amount of rows that you want to search through. Some SQL Server logs tend to get pretty big (over 100.000 rules a day with some of my servers). The parameter is not mandatory but will be set to 100 if no value is given.

The script uses a script that I posted earlier to ping the host. This script can be found here.

In case the output parameter is used, the script will create a CSV file with the following format:

The servername will be server from the server list. The second part will indicate if an error, warning or no error has been presented. The last part will show why a certain warning occured.

The code:

Hope you enjoy the script!


5 thoughts on “Get SQL Server Logs with Powershell

  1. AKP

    Thanks for your great script. But when i tried to run it as
    .\Get_SQLLog2.ps1 -ServerList ‘C:\Temp\Servers.txt’ -Output ‘C:\temp’

    It is giving me following error message:

    INFO: CSV file will be written to C:\temp\Report_DailyCheck_Log_20110831150133.csv.
    Exception calling “Send” with “1” argument(s): “Value cannot be null.
    Parameter name: hostNameOrAddress”
    At F:\docs\PS Scripts\Function_Ping-Host.ps1:19 char:16
    + if ($ping.send <<<< ($H).status -eq "Success")
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

    Any idea, much more appreciated.

  2. Sean

    I like this script very much but when I run it I get the error that server can not be pinged. I have my Servers.txt file and it contains one servername without semicolons around it. Not sure how to get it to work? If you can offer any help I would greatly appreciate it. With regards.

    • Sander Stad

      Can you send me your servers.txt file. Maybe there is something wrong in there.
      Send it to “sanderstad at gmail dot com”

  3. vinod

    Hi I would like to have the script with send mail feature of the ping servers and error log . Appreciate if i could get with that modification.

Leave a Reply