Solved

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

Posted on 2016-09-07
6
98 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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

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

Industry Leaders: 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!

Question has a verified solution.

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

Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

729 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