question

JoeFrish-7036 avatar image
0 Votes"
JoeFrish-7036 asked cooldadtx answered

Powershell - to notify of files in multiple folders older than 10 mins

I have a powershell script that's checking a single directory and notifying me if a file exists and it works great:

 $file='C:\temp\*.pdf'
 $Logfile = 'C:\PicksheetMonitoring\Monitor.log'
 $Counter = 0
    
 function logit($msg)
 {
     $msg='[{0}]{1}' -f [datetime]::Now, $msg
     $msg|Out-File $logfile -append
 }
    
 foreach ($file in (Get-Item $file)) {
           
 if([datetime]::Now - (Get-Item $file).LastWriteTime -gt [timespan]'0:0:20:0')
 {
     $counter = $counter+1
    }
  }
  if ($counter -gt 0)
  { send-mailmessage -to etc etc
 }


I want to do the same thing, but I want it to scan every subfolder in a parent folder and tell me if any files exist that are more than 10 minutes old, and also email me which files those are... I got this thing to work once... but I haven't been able to get it to work since:


 $file = "$Folder\*.pdf"
 $Logfile = 'C:\folder\Monitor.log'
 $Counter = 0
 $AllFolders = Get-ChildItem E:\Imports\ -Recurse -Directory | % { $_.fullname }
 foreach ($Folder in $AllFolders) 
 {
 function logit($msg)
 {
     $msg='[{0}]{1}' -f [datetime]::Now, $msg
     $msg|Out-File $logfile -append
 }
    
 foreach ($file in Get-Item $file) {
           
 if([datetime]::Now - (Get-Item $file).LastWriteTime -gt [timespan]'0:0:10:0')
 {
     $counter = $counter+1
    }
  }
  if ($counter -gt 0)
  {
 Send-Message -to  etc etc..
 } 
 }

Any idea's what I'm missing?


windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

cooldadtx avatar image
0 Votes"
cooldadtx answered JoeFrish-7036 edited

How about just combining all that into a single query.

$searchPath = "Somedirectory"
$olderThan = '00:10:00' # 10 minutes

# Get all files, recursively in the search path that are of the given type and are older than the given interval
$files = Get-ChildItem $searchPath -include '*.pdf' -recurse | Where-Object { [DateTime]::Now - $_.LastWriteTime -gt olderThan }
foreach ($file in $files) {
   # Now what?
}
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thats close, but I do want my alerted email to contain the path of the file it found, as well as how many files it has. I may be able to handle that based on the query you gave me :)
Thank you

0 Votes 0 ·

Okay... maybe I can use a hand lol...
(I did fix a typo, you missed the $ on the "olderthan" in your query.)
But, I'm not sure how to grab the full file path from the for each as a variable for each email sent:

 $FullPathoffile = ?
 $searchPath = "E:\path"
 $olderThan = '00:10:00' # 10 minutes
 # Get all files, recursively in the search path that are of the given type and are older than the given interval
 $files = Get-ChildItem $searchPath -include '*.pdf' -recurse | Where-Object { [DateTime]::Now - $_.LastWriteTime -gt $olderThan }
 foreach ($file in $files) {
    #What to do for each file
    echo $file
  {
      $counter = $counter+1
     }
   }
   if ($counter -gt 0)
    { Send-Message
 }
0 Votes 0 ·

The only other thing is, I'd end up getting an email for every PDF it finds, I only want a single email for each folder a PDF is found in, so the query would probably need to be a little different...

0 Votes 0 ·
IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered

Hi,

You could try testing $files.count.

 $searchPath = "E:\path"
 $olderThan = '00:10:00' # 10 minutes
 # Get all files, recursively in the search path that are of the given type and are older than the given interval
 $files = Get-ChildItem $searchPath -include '*.pdf' -recurse | Where-Object { [DateTime]::Now - $_.LastWriteTime -gt $olderThan }
 if($files.count -gt 0){
     Send-MailMessage
 }

Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

JoeFrish-7036 avatar image
0 Votes"
JoeFrish-7036 answered JoeFrish-7036 edited

This works for what I need, I'll just get a lot of emails if the folders fill up quickly:

 $olderThan = '00:10:00' # 10 minutes
 # Get all files, recursively in the search path that are of the given type and are older than the given interval
 $files = Get-ChildItem E:\Imports -include '*.pdf' -recurse | Where-Object { [DateTime]::Now - $_.LastWriteTime -gt $olderThan} | % { $_.FullName }
 foreach ($file in $files) {
    #What to do for each file
   #echo $file
   Send-MailMessage -to
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

cooldadtx avatar image
0 Votes"
cooldadtx answered

If you want to do something at each folder level then you can either change the query back to getting folders first or group by parent after you have the files.

$folders = Get-ChildItem $searchPath -directory -recurse
foreach ($folder in $folders)
{
   # Get the desired files
   $files = Get-ChildItem $folder -include '*.pdf' | Where-Object { [DateTime]::Now - $_.LastWriteTime -gt olderThan }
   if ($files.Length -gt 0) {
       # $files has the files in the folder $folder, do what you need to
   }
}
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.