Solved

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

Posted on 2016-09-07
6
19 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
  • 3
  • 3
6 Comments
 
LVL 83

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 83

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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:IM&T SRFT
ID: 41787730
Hi

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

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

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

Suggested Solutions

Why would I want to create a function for tracking messages? I am glad you asked. As with most monotonous/routine tasks, human error tends to creep in after doing the same task over and over again. By creating a function, you load the function once…
Synchronize a new Active Directory domain with an existing Office 365 tenant
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

762 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