Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2016-09-07
6
Medium Priority
?
186 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

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.

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.
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.
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…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

704 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