Solved

PowerShell - Count of files in a given directory and email alerting

Posted on 2016-09-07
6
120 Views
Last Modified: 2016-09-07
Hi All

I would like to have a Power Shell script that monitors a folder directory and outputs the count of files (excluding folders containing more files) and outputs to email.

E.G. I have a directory here - C:\MATSTEST\Countoffiles which contains 2 files and then 1 folder which contains 3 further files.

I want to be able to run a script on this folder and for it to email alert advising there are 2 files in that directory.

Does that make sense?

I have quite a large number of these directories that need monitoring across different servers/locations but hopefully if I can get the general understanding of how to do this for 1 directory should be able to work it out and output to a grid for example
0
Comment
Question by:IM&T SRFT
[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
  • 3
  • 3
6 Comments
 
LVL 85

Expert Comment

by:oBdA
ID: 41787592
Assuming PS 3.0 or later:
$Folder = "C:\MATSTEST\Countoffiles"
$From = "from@domain.com"
$To = "to@domain.com"
$Subject = "FileCount"
$Body = "File count for '$($Folder)': " + (Get-ChildItem -Path $Folder -File | Measure-Object).Count
$SMTPServer = "smtp.domain.com"
Send-MailMessage -From $From -to $To -Subject $Subject -Body $Body -SmtpServer $SMTPServer

Open in new window

0
 

Author Comment

by:IM&T SRFT
ID: 41787695
Thank you very much,

If this is too cheeky then I can post another question but when searching the internet (clearly using the wrong phrases) I can not find what I need list say 2-3 different directories to check without using a repetitive coding.
0
 
LVL 85

Expert Comment

by:oBdA
ID: 41787702
All supposed to go into a single mail, or different mails for each folder?
And if the latter, for different recipients depending on the folder, or just individual mails?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:IM&T SRFT
ID: 41787730
Hi

For these it would be the same email and recipient
0
 
LVL 85

Accepted Solution

by:
oBdA earned 500 total points
ID: 41787759
$FolderList = @(
	"C:\Temp\Delete Me"
	"C:\Temp\DeleteMe"
)
$Body = ($FolderList | ForEach-Object {
	"File count for '$($_)': " + (Get-ChildItem -Path $_ -File -ErrorAction SilentlyContinue | Measure-Object).Count
}) -join "`r`n"

$From = "from@acme.com"
$To = "to@acme.com"
$Subject = "FileCount"
$SmtpServer = "smtp.acme.com"
Send-MailMessage -From $From -to $To -Subject $Subject -Body $Body -SmtpServer $SmtpServer

Open in new window

0
 

Author Closing Comment

by:IM&T SRFT
ID: 41787922
Once again

Thank you for the help on this issue
0

Featured Post

Ready to trade in that old firewall?

Whether you need to trade-up to a shiny new Firebox or just ready to upgrade from whatever appliance you're using now, WatchGuard has the right appliance for you! Find your perfect Firebox today with appliance sizing tool!

Question has a verified solution.

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

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

623 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