We are working on a folder monitor script, and have used some other examples found in Thwack discussions to help build our solution. The goal of this script is to check a network folder and determine if there has been a new file within the last 20 minutes. This is a solution that was the best way to monitor an application, to ensure it is processing files as expected. The idea is, if there hasn't been a new file created in the directory for 20 minutes, raise an alert for team members to inspect.
The script is working in PowerShell from one of the Polling engines in our environment, but when we copy it in to a PowerShell component, it is failing due to syntax. Are there any adjustments that can be made so this executes correctly in SolarWinds? We are very close to what should be our solution, just need some help with getting this to run.
Thanks all!
In the attached SAM template it is the 4th template "PS to check files for last 20 minutes"
# Define the directory to monitor
$directoryPath = "NETWORKPATH"
# Define the time interval (20 minutes)
$timeInterval = New-TimeSpan -Minutes 20
# Get the current time
$currentTime = Get-Date
# Get the most recent file creation time in the directory
$latestFile = Get-ChildItem -Path $directoryPath | Sort-Object CreationTime -Descending | Select-Object -First 1
if ($latestFile) {
$latestFileCreationTime = $latestFile.CreationTime
# Calculate the time difference
$timeDifference = $currentTime - $latestFileCreationTime
# Check if the time difference is greater than the defined interval
if ($timeDifference -gt $timeInterval) {
# Alert: No new file created in the last 20 minutes
#Write-Host "Alert: No new file has been created in the directory '$directoryPath' for the last 20 minutes."
$stat = 1
Write-host "Statistic.Stat: $stat"
} else {
#Write-Host "A new file has been created within the last 20 minutes."
$stat = 0
Write-host "Statistic.Stat: $stat"
}
}