System.io.filesystemwatcher does not work in windows 2022

Hi,
I am a beginner in powershell scripting. For the following script:

(to look for a keyword in the log file that is being generated by another process and if it matches, do something)

# Define the path to the log file and the keyword to search for
$logFilePath = "c:\Temp\"
$keyword = "123"

# Define the action to take when an exception is logged
$action = {
    param ($source, $e)
    $filePath = $e.FullPath
    if (Select-String -Path $filePath -Pattern '123') {
        Write-Host "Exception detected in $filePath. Starting Resource Monitor..."
        #do something
        exit
    } else {
        Write-Output "No new exceptions detected in $filePath."
    }
}

# Create a FileSystemWatcher to monitor the log file for changes
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = [System.IO.Path]::GetDirectoryName($logFilePath)
$watcher.Filter = [System.IO.Path]::GetFileName($logFilePath)
$watcher.NotifyFilter = [System.IO.NotifyFilters]::LastWrite
$watcher.IncludeSubdirectories = $true

# Register the event handler
Register-ObjectEvent $watcher "Changed" -Action $action

# Start monitoring
$watcher.EnableRaisingEvents = $true

# Keep the script running indefinitely
Write-Output "Monitoring $logFilePath for exceptions. Press Ctrl+C to stop."
while ($true) {
    Start-Sleep -Seconds 10
}

It works in windows 10, but doesn’t work in windows 2022.

The script will never trigger the Action in Windows 2022.

Any help will be appreciated.

I could not replicate the issue. The only issue I found was that exit command, that breaks the event monitoring after one action. I also recommend capturing the event object so you can unregister it when you’re done

$logFilePath = "c:\Temp\"
$keyword = "123"

# Define the action to take when an exception is logged
$action = {
    param ($source, $e)
    $filePath = $e.FullPath
    if (Select-String -Path $filePath -Pattern '123') {
        Write-Host "Exception detected in $filePath. Starting Resource Monitor..."
        #do something
        #exit <--- this is a problem
    } else {
        Write-Host "No new exceptions detected in $filePath."
    }
}

# Create a FileSystemWatcher to monitor the log file for changes
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = [System.IO.Path]::GetDirectoryName($logFilePath)
$watcher.Filter = [System.IO.Path]::GetFileName($logFilePath)
$watcher.NotifyFilter = [System.IO.NotifyFilters]::LastWrite
$watcher.IncludeSubdirectories = $true

# Register the event handler
$objevent = Register-ObjectEvent $watcher "Changed" -Action $action

# Start monitoring
$watcher.EnableRaisingEvents = $true

# Keep the script running indefinitely
Write-Output "Monitoring $logFilePath for exceptions. Press Ctrl+C to stop."
while ($true) {
    Start-Sleep -Seconds 10
}

$objevent | Get-EventSubscriber | Unregister-Event

Thanks for your reply.

Is it possible the script runs for a while, then it stops working? This is what I noticed.

To be specific, the process is writing log files in "c:\User\Public\software\jobs\(yyy)"

yyy is a GUID and it’s random.
My $logFilePath is: c:\User\Public\software\jobs

I used “IncludeSubdirectores=$true” and think should be ok.

How long is a while? I had a test running for about 10 minutes and it still reflected changes.

On that one Server 2022, I stopped the script, and came back the next day it stopped working. Whereas I moved the script to my windows 10 and it is working fine even after few days.
On the other hand, does it make any difference to the filesystemwatcher by manually writing and saving a to file, than another service (it’s a java program, specifically) opening and writing to log files?

It’s watching for files changing, not what’s changing them. You definitely need to try my suggestion with the exit command removed. Perhaps that solves the issue you’ve seen.

It seems working consistently now. Thank you!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.