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.
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
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.