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?