• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5470
  • Last Modified:

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....
0
santoshmotwani
Asked:
santoshmotwani
  • 4
  • 3
  • 2
  • +1
1 Solution
 
prashanthdCommented:
we need to check file last modified date or created date?
0
 
prashanthdCommented:
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

0
 
santoshmotwaniAuthor Commented:
@prashanthd

creation date

I will try it and will let you know thanks
0
 The Evil-ution of Network Security Threats

What are the hacks that forever changed the security industry? To answer that question, we created an exciting new eBook that takes you on a trip through hacking history. It explores the top hacks from the 80s to 2010s, why they mattered, and how the security industry responded.

 
prashanthdCommented:
Made changes to check on file creation time...
$src="c:\test\"
$sendmail=$false

 Get-ChildItem -path $src -Recurse |
 Foreach-Object { 
 	  #write-host $_.fullname
	  $dtdiff = New-TimeSpan ($_.CreationTime) $(Get-Date)
	  
	  if ($dtdiff.minutes -gt 10){
	  	$strbody=$strbody +$_.fullname+ " - Created Time: "  +$_.CreationTime +"`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

0
 
santoshmotwaniAuthor Commented:
this works good....
how can i have this running 24 X 7 ?
0
 
santoshmotwaniAuthor Commented:
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 .

http://www.experts-exchange.com/Programming/Languages/Scripting/Powershell/Q_26902296.html
0
 
santoshmotwaniAuthor Commented:
Thnx Heaps
0
 
Nachman WeissCommented:
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
0
 
Mark HueyCommented:
Is there a way to run this against multiple folders?
0
 
Mark HueyCommented:
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

0

Featured Post

What Security Threats Are We Predicting for 2018?

Cryptocurrency, IoT botnets, MFA, and more! Hackers are already planning their next big attacks for 2018. Learn what you might face, and how to defend against it with our 2018 security predictions.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now