Solved

Powershell script to notify files older than 10 mins

Posted on 2011-03-17
10
4,393 Views
Last Modified: 2016-10-26
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
Comment
Question by:santoshmotwani
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 12

Expert Comment

by:prashanthd
ID: 35163151
we need to check file last modified date or created date?
0
 
LVL 12

Expert Comment

by:prashanthd
ID: 35163314
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
 
LVL 16

Author Comment

by:santoshmotwani
ID: 35163983
@prashanthd

creation date

I will try it and will let you know thanks
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 12

Accepted Solution

by:
prashanthd earned 500 total points
ID: 35164336
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
 
LVL 16

Author Comment

by:santoshmotwani
ID: 35185270
this works good....
how can i have this running 24 X 7 ?
0
 
LVL 16

Author Comment

by:santoshmotwani
ID: 35186134
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
 
LVL 16

Author Closing Comment

by:santoshmotwani
ID: 35186135
Thnx Heaps
0
 

Expert Comment

by:Nachman Weiss
ID: 41815306
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
 

Expert Comment

by:Mark Huey
ID: 41860283
Is there a way to run this against multiple folders?
0
 

Expert Comment

by:Mark Huey
ID: 41860357
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
This tutorial will walk an individual through configuring a drive on a Windows Server 2008 to perform shadow copies in order to quickly recover deleted files and folders. Click on Start and then select Computer to view the available drives on the se…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question