?
Solved

Powershell script to notify files older than 10 mins

Posted on 2011-03-17
10
Medium Priority
?
5,046 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Accepted Solution

by:
prashanthd earned 2000 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

Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

Question has a verified solution.

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

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

765 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