Exporting SQL Data to EventSentry Log Files via PowerShell

Article ID: 557
Category: Integration
Updated: 2026-06-10

This article explains how to run a custom database query on a schedule, write the results to a log file, and surface that data in EventSentry's Web Reports.

The setup uses a Windows System DSN to connect to a database, which means it works with any ODBC-supported engine — including SQL Server, PostgreSQL, MySQL and others. The steps below use PostgreSQL (psqlODBC driver) as an example, but the EventSentry configuration is identical regardless of the backend. If you are connecting to a SQL Server database instead, create the System DSN using the ODBC Driver XX for SQL Server.

The example script queries all error events from the EventSentry database and writes them to a .log file. Each run produces a new timestamped file, so EventSentry treats every execution as a fresh log entry.

A – Set Up a System DSN

The PowerShell script connects to the database through an ODBC System DSN. This must be configured before the script can run.

  1. Open odbcad32.exe.
  2. Go to the System DSN tab > click Add > select PostgreSQL Unicode.
  3. Fill in the database connection details (Data Source name, Server, Port, Database, User Name, Password).
  4. Click Test to verify the connection, then click Save.

B – Create and Test the PowerShell Script

The script connects to the database via the System DSN, runs a query, and writes the results to a timestamped log file. Remember to:

  • Reference the System DSN: Set the DSN variable to the name you created in section A.
    $dsn = "EventSentryPG"

  • Set the Output Directory: Define the folder where the script will write its output files.
    $outputDir = "C:\Scripts\Logs"

  • Create Timestamped Output Files: Each run generates a uniquely named file so EventSentry picks it up as a new log entry.
    $timestamp = Get-Date -Format "yyyy.MM.dd_HH.mm.ss"
    $outputFile = Join-Path $outputDir "ESErrors_$timestamp.log"

  • Test the Script: Run the script manually and confirm that a new file appears in the output directory for each execution. The file name should reflect the current timestamp.

You can use the following PowerShell script as an example:

C – Set Up a User Embedded Script

Once the script is working, add it to EventSentry as a User Embedded Script so it can be called by an Action.

  1. In the Management Console, go to Scripts > User (Embedded). Right-click and select Add Script, or click the Add (+) button in the ribbon.
  2. Give the script a descriptive name (for example, Error_Events) and paste your script content into the Script Content field.
  3. In the Interpreter drop-down, select: powershell.exe -inputformat none -executionpolicy bypass -file

D – Define the Log File to Monitor

Configure EventSentry to recognize the output files generated by the script.

  1. In the Management Console, go to Packages > Log Files & APIs > Define Files and File Types.
  2. In the Log Files section, click + to add a File entry. Give it a descriptive name and select Non-Delimited as the file type (*).
  3. Set the path to the output directory, using a wildcard to match all timestamped files: C:\Scripts\Logs\ESErrors_*.log The * wildcard matches any timestamp in the file name, so every file the script creates will be picked up automatically.

E – Create a Log Files & APIs Package

  1. Right-click Log Files & APIs and select Add Package. Give it a descriptive name (for example, PowerShell Scripts).
  2. Click the new package and select Add New File. Choose the file definition created in section D and click OK.
  3. Click the file entry inside the package. In the Database Consolidation tab, select the destination database where the log entries will be stored. Configure the Include/Exclude filters as needed.
  4. Right-click the package and assign it to the target host — the machine where the script runs.

Note: Step 3 is required if you want the log data to appear in Web Reports.

F – Create an Application Scheduler Package

  1. Go to Packages > System Health, right-click, and select Add Package. Name it (e.g., Error_Events).
  2. Right-click the newly created package and select Add > Application Scheduler.
  3. Inside the Application Scheduler settings, click the + (plus) symbol. In the Filename field, select the user-embedded script created in Step C. Embedded scripts are listed with an @ prefix.
  4. Set up the desired schedule for the script to run. You can use the Test option to ensure the script is working correctly, then click OK.

  5. Assign the package to the desired host (the machine where the script will run).

G – Save the Configuration

Save the new configuration by clicking Save in the ribbon (Home > Save). EventSentry will deploy the changes to the assigned host and begin executing the script on the defined schedule.

Verifying the Results in the Web Reports

To confirm, go to the Web Reports > Features > Log Files. Use the summary view to locate the log, or search by the file name pattern defined in section D.
Each scheduled run should produce a new entry in the log file list, with the query results visible as individual log lines.


More information and related articles:

Defining Monitored Files
EventSentry Training [09-01]: Non-Delimited Logs / Log File Monitoring
EventSentry Training [09-02]: Delimited Logs / Log File Monitoring
How to monitor data in a SQL database



Try EventSentry on-premise

FREE 30-day evaluation

Download Now