We are testing for file(s) existence in folder with *.pdf that are older than 1560 minutes. The powershell script works fine when run on server.
It does not work when running from SAM GET SCRIPT OUTPUT.
Any help is appreciated.
Looks like me like its returning a null, you need to edit it to return a 0 when no files are found that match the criteria.
Yes, it's returning Not a Number(NaN). This powershell script works on Solarwinds server but not via EDIT SCRIPT. Looking for a fix.
Have tried setting the value $stat = 0, $stat="0", and $stat='0' none of these worked.
Found a fix
Had to change
if ($FileArray.Count -eq 0)
{
$msg = "No file found over $($minutes) minutes old"
}
to
if (($FileArray | Measure-Object).count = "0")
And no, did not have to change line below. This seemed to work in the code when there were files.
$stat=$FileArray.Count
Below is updated code for File Age Monitor with Wildcard
param([string]$path, [int]$minutes)
# Created by Thwack user Chad.Every. See https://thwack.solarwinds.com/docs/DOC-190598 for more detail.
# Pass in the directory path including a * with the extension type you want to check. Then pass in the number of minutes old that file needs to be.
#Example, change this path to the folder/file type you wish to monitor. Wildcard in file name and extension are supported.
#$path="c:\temp\*.txt"
$stat=0
$msg=""
$FileArray=""
try
$LastWrite = $(Get-Date).AddMinutes(-$minutes)
#If you want to include sub folders comment the next line and uncomment the following line
$FileArray = $(Get-ChildItem $path | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $LastWrite })
#$FileArray = $(Get-ChildItem $path -Recurse | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $LastWrite })
else
$msg = "$($stat) files found older than $($minutes) minutes: "
foreach ($file in $FileArray)
$msg+=" " + $file + ","
catch
$stat=1
$msg=$_.Exception.Message
Write-Host "Statistic: $stat"
Write-Host "Message: $msg"
exit 0