Notify when text files contain a pattern using PowerShell and RegEx

The PowerShell Data Provider in exMon can be used for various things. In this post I will create a test in exMon, using PowerShell and regular expression, to find all lines in text files that contain a specific pattern. In this case, we will look for all lines that contain two or more semi commas.


1. Create a new Query in exMon Administrator by right clicking the Tests folder and select the Data Provider: PowerShell

2. In the query window add the following PowerShell script:

# The files to check for
$path = "C:\temp\Names*.txt"
# The regex pattern to look for. This case, two or more semicommas in a line
$regex = "(.*);(.*);(.*)"

# Create the result DataTable
$exMonResult= New-Object system.Data.DataTable

$exMonResult.columns.add(($cFileName = New-Object system.Data.DataColumn FileName,([string])))
$exMonResult.columns.add(($cLineNum = New-Object system.Data.DataColumn LineNum,([int])))
$exMonResult.columns.add(($cText = New-Object system.Data.DataColumn Text,([string])))

# Loop through the files
$files = Get-ChildItem $path
foreach ($file in $files) 
    # Loop through each line in the file
    $lineNum = 0
    foreach($line in Get-Content $file) {

        # If we match, add to our results
        if($line -match $regex){
            $row = $exMonResult.NewRow();

            $row.FileName = $file;
            $row.LineNum = $lineNum;
            $row.Text = $line;


3. Change the $path variable to match your files

4. Change the $regex variable to match your regular expression pattern.

5. Configure email settings and schedule as normal.

Example email from the test:



exMon Contact Us