Link to home
Start Free TrialLog in
Avatar of santoshmotwani
santoshmotwaniFlag for Australia

asked on

Powershell script to notify files older than 10 mins

Hi Experts,

I have a production server executing files continuously . Sometimes i get files which are not in proper format hence they are not processed and they stay in there forever. I would like to have a script that will run every 5 mins on a specific folder to check for folder with a file older than 10 mins . It may be possible there are empty folders , i will only like folder with a file in it.

I would love to have a notification via email but if not a pop up box will do the job.

Please Help....
Avatar of prashanthd
prashanthd
Flag of India image

we need to check file last modified date or created date?
Hi,

Try the following code, modify the following in code

$src="c:\test\"

$strFromAddress = "abc@xyz.com"
$strToAddress = "abc@xyz.com"
$strMessageSubject = "REPORT"
$strMessageBody = $strbody
$strSendingServer = "smtpserver"
$src="c:\test\"
$sendmail=$false

 Get-ChildItem -path $src -Recurse |
 Foreach-Object { 
 	  #write-host $_.fullname
	  $dtdiff = New-TimeSpan ($_.LastWriteTime) $(Get-Date)
	  
	  if ($dtdiff.minutes -gt 10){
	  	$strbody=$strbody +$_.fullname+ " - Last Modified Time: "  +$_.LastWriteTime +"`r`n"
		$sendmail=$true
		}		
}
#$strbody
if($sendmail -eq $true){
# Email components
$strFromAddress = "abc@xyz.com"
$strToAddress = "abc@xyz.com"
$strMessageSubject = "REPORT"
$strMessageBody = $strbody
$strSendingServer = "smtpserver"

# Email objects
$objSMTPMessage = New-Object System.Net.Mail.MailMessage $strFromAddress, $strToAddress, $strMessageSubject, $strMessageBody
$objSMTPClient = New-Object System.Net.Mail.SMTPClient $strSendingServer
$objSMTPClient.Send($objSMTPMessage)
}

Open in new window

Avatar of santoshmotwani

ASKER

@prashanthd

creation date

I will try it and will let you know thanks
ASKER CERTIFIED SOLUTION
Avatar of prashanthd
prashanthd
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
this works good....
how can i have this running 24 X 7 ?
i am able to run this now as a scheduled task . i created a bat file .

I am posting another question similar to this . Will appreciate your help .

https://www.experts-exchange.com/questions/26902296/Powershell-script-to-notify-only-new-files.html
Thnx Heaps
Avatar of Nachman Weiss
Nachman Weiss

HI There i was trying to use this script  but i couldn't figure out how to get the email part to work ?

do i need to have a smtp relay server setup ?

or is there a way for me to relay through yahoo or gmail and using the authentication protocols ?

Thanks
Nachman
Is there a way to run this against multiple folders?
I think I figured it out. I just added a second src and duplicated the file checking script like below.

$src1="c:\test\targetfolder\"
$src2="c:\test\targetfolder2\"
$sendmail=$false



 Get-ChildItem -path $src1 -Recurse |
 Foreach-Object { 
 	  #write-host $_.fullname
	  $dtdiff = New-TimeSpan ($_.CreationTime) $(Get-Date)
	  
	  if ($dtdiff.minutes -gt 5){
	  	$strbody=$strbody +$_.fullname+ " - Last Modified Time: "  +$_.LastWriteTime +"`r`n"
		$sendmail=$true
		#write-host $_.fullname
		}		
}

Get-ChildItem -path $src2 -Recurse |
 Foreach-Object { 
 	  #write-host $_.fullname
	  $dtdiff = New-TimeSpan ($_.CreationTime) $(Get-Date)
	  
	  if ($dtdiff.minutes -gt 5){
	  	$strbody=$strbody +$_.fullname+ " - Last Modified Time: "  +$_.LastWriteTime +"`r`n"
		$sendmail=$true
		#write-host $_.fullname
		}		
}


#$strbody
if($sendmail -eq $true){
# Email components
$strFromAddress = "filechecker@host.com"
$strToAddress = "myemail@host.com"
$strMessageSubject = "Stuck PO Files Report"
$strMessageBody = $strbody
$strSendingServer = "smtpserver"

# Email objects
$objSMTPMessage = New-Object System.Net.Mail.MailMessage $strFromAddress, $strToAddress, $strMessageSubject, $strbody
$objSMTPClient = New-Object System.Net.Mail.SMTPClient $strSendingServer
$objSMTPClient.Send($objSMTPMessage) 

}

Open in new window