Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2016-09-07
6
Medium Priority
?
254 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 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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 

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 2000 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

WatchGuard Case Study: NCR

With business operations for thousands of customers largely depending on the internal systems they support, NCR can’t afford to waste time or money on security products that are anything less than exceptional. That’s why they chose WatchGuard.

Question has a verified solution.

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

My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
Measuring Server's processing rate with a simple powershell command. The differences in processing rate also was recorded in different use-cases, when a server in free and busy states.
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 anti-spam), the admin…
Loops Section Overview

885 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