4 Replies Latest reply on Feb 12, 2019 3:08 PM by dpillar@summit.com

    File Age Monitor wildcard

    dpillar@summit.com

      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.

        • Re: File Age Monitor wildcard
          mesverrum

          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.

          • Re: File Age Monitor wildcard
            dpillar@summit.com

            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")

                {

                    $msg = "No file found over $($minutes) minutes old"

                }

             

             

            And no, did not have to change line below. This seemed to work in the code when there were files.

            $stat=$FileArray.Count

              • Re: File Age Monitor wildcard
                dpillar@summit.com

                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 })

                 

                 

                 

                 

                    if (($FileArray | Measure-Object).count = "0")

                    {

                        $msg = "No file found over $($minutes) minutes old"

                    }

                    else

                    {

                        $stat=$FileArray.Count

                        $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