Forum Discussion

Tim_OShea's avatar
Tim_OShea
Icon for Neophyte rankNeophyte
8 months ago

Monitoring for file updates

We have a windows share that contains a number of document templates. I would like to be alerted if/when any of these templates is updated, and get an alert containing the name of the updated template. (It is reasonable for these templates to be updated, but can cause issues if this is not expected)

Is this possible? Is there an existing datasource that could do this?

  • Anonymous's avatar
    Anonymous

    Since you want the name of the file that changed and these files a probably not plain text files, you'd simply do a multi-instance, batchscripted datasource. How you write the path will depend on how the share is accessed by the collector. You can use a local path or a UNC path, just make sure that the credentials used to run the collector process have access to the path.

    Your discovery script would look like this:

    # set a property called `targetpath` on the device containing the path to the share. 
    # i.e.: '\\localhost\C$\Users\sweenig\3D Objects\*'
    $path = '##targetpath##'
    Get-Item $path | ForEach-Object {
        Write-Host "$($_.Name.Replace(' ',''))##$($_.Name.Replace(' ',''))"
    }

    Then your collection script would look like this:

    # set a property called `targetpath` on the device containing the path to the share.
    # i.e.: '\\localhost\C$\Users\sweenig\3D Objects\*'
    $path = '##targetpath##'
    $currentDateTime = Get-Date
    $files = Get-ChildItem -path $path
    foreach ($file in $files){
        $lastWriteTime = $file.LastWriteTime
        $hoursSinceModified = ($currentDateTime - $lastWriteTime).TotalHours
        Write-Host "$($file.Name.Replace(' ','')).lastModifiedHoursAgo: $($hoursSinceModified)"
    }

    Then create a datapoint using "Content the script writes to standard output" and "multi-line key-value pairs" and where the key is "##WILDVALUE##.lastModifiedHoursAgo".