Solved

Powershell script to notify files older than 10 mins

Posted on 2011-03-17
10
4,155 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

New Windows 7 Installations take days for Windows-Updates to show up and install. This can easily be fixed. I have finally decided to write an article because this seems to get asked several times a day lately. This Article and the Links apply to…
The recent Microsoft changes on update philosophy for Windows pre-10 and their impact on existing WSUS implementations.
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…
This tutorial will walk an individual through the steps necessary to install and configure the Windows Server Backup Utility. Directly connect an external storage device such as a USB drive, or CD\DVD burner: If the device is a USB drive, ensure i…

758 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now