The PowerShell Data Provider in exMon can be used for various things. In this post I will create a test in exMon, using PowerShell, to watch a directory and find all files that have not been modified for the past 31 days.

 

1. Create a new Query in exMon Administrator by right clicking the Tests folder


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

# Check for files older than $Days in the folder $TargetFolder
$Days = "31"
$TargetFolder = "C:\temp"

 

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

 

# Create column definition
$fileName = New-Object system.Data.DataColumn fileName,([string])
$exMonResult.columns.add($fileName)
$modifiedDate = New-Object system.Data.DataColumn modifiedDate,([datetime])
$exMonResult.columns.add($modifiedDate)

 

$Now = Get-Date
$LastWrite = $Now.AddDays(-$days)

 

# Find the files
$Files = Get-ChildItem $TargetFolder | Where { $_.LastWriteTime -le $LastWrite } | sort-object LastWriteTime
foreach ($File in $Files)
{
    # Add Row
    $row = $exMonResult.NewRow();
    $row.fileName = $file.name;
    $row.modifiedDate = $file.LastWriteTime;
    $exMonResult.Rows.Add($row);
}

3. Change the $TargetFolder variable to your directory

4. Configure email settings and schedule as normal.

 

That’s it. Now you have a test notifies you of all files that have not been modified for the past 31 days.