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

Joe Frish 6 Reputation points
2021-06-15T19:08:18.16+00:00

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 for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Michael Taylor 60,161 Reputation points
    2021-06-15T19:50:26.557+00:00

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

  2. Anonymous
    2021-06-16T13:03:29.87+00:00

    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.

    0 comments No comments

  3. Joe Frish 6 Reputation points
    2021-06-16T14:15:32.117+00:00

    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
    
    0 comments No comments

  4. Michael Taylor 60,161 Reputation points
    2021-06-16T18:30:50.207+00:00

    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
       }
    }
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.