Link to home
Create AccountLog in
Avatar of jimmylew52
jimmylew52Flag for United States of America

asked on

Powershell script sends email even when folder has no files in it

I have been getting a lot help with this from the forum but am lost and needing more assistance. I have the following script, thanks to this forum, that email me a list of files in a folder. the original script failed when the folder did not have any files in it. Since adding the text portion to the script the script sends an email even if the folder does not have any files in it. Many thanks for any help.

Original:
Send-MailMessage -From 'files_on_16@mycompany.com' -To 'me@mycompany.com' `
  -SmtpServer 'smtp.mycompany.com' `
  -Subject 'Files Found in the folder on Server16.' `
  -Body  ((Get-ChildItem "E:\" | ? {! $_.PsIsContainer} | ft -auto name, CreationTime | out-string) -join "`n")

After adding the text:
Send-MailMessage -From 'files_on_16@mycompany.com' -To 'me@mycompany.com' `
  -SmtpServer 'smtp.mycompany.com' `
  -Subject 'Files Found in the folder on Server16.' `
  -Body  $("This is a Test mail `n The file list `n"+`
      ((Get-ChildItem "E:\" | ? {! $_.PsIsContainer} | ft -auto name, CreationTime | out-string) -join "`n"))
Avatar of SubSun
SubSun
Flag of India image

Try..

If(Get-ChildItem "E:\Test" | ? {! $_.PsIsContainer})
{Send-MailMessage -From 'files_on_16@mycompany.com' -To 'me@mycompany.com' `
  -SmtpServer 'smtp.mycompany.com' `
  -Subject 'Files Found in the folder on Server16.' `
  -Body  $("This is a Test mail `n The file list `n"+`
      ((Get-ChildItem "E:\Test" | ? {! $_.PsIsContainer} | ft -auto name, CreationTime | out-string) -join "`n"))
}

Open in new window

Avatar of jimmylew52

ASKER

Thank You.

This sends the email even if there is not a file in the folder.
Is there any difference in the list of files you see in both emails ? (Email sent with files and email sent without files)
Yes, The folder with files lists the files, the folder without files does not have a list of files just the text.
This may be working

$fileCount = (Get-ChildItem E:\Test2).count
  If ($fileCount -gt 0)
  {Send-MailMessage -From 'files@mycompany.com' -To 'me@mycompany.com' `
  -SmtpServer 'smtp.mycompany.com' `
  -Subject 'Files Found' `
  -Body  $("The Following Files Were found On `n server 16 Test2 `n" +`
     ((Get-ChildItem "E:\Test2"`
     | ? {! $_.PsIsContainer} | ft -auto name, CreationTime | out-string) -join "`n"))`
}
SOLUTION
Avatar of Rajitha Chimmani
Rajitha Chimmani
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
The code which I posted (ID: 38889616) will send mail only if there is a file in folder..
Sorry Subsun, your code sent an email even if the folder was empty. Looks like it should have worked but did not.

Rajitha14

how would it be different using getfiles()? I am having problems when I use the code on the production server. My code works fine on the test server but sends an email even is the folder does not have any files in the folder on the production server.

Baffled.
I just tested and it's working as expected.. Can you post the code which you are using?
Do you mean that the count parameter you used also results in same output? What is the problem you have on production server?
If(Get-ChildItem "E:\Test" | ? {! $_.PsIsContainer})
{Send-MailMessage -From 'files_on_16@mycompany.com' -To 'me@mycompany.com' `
  -SmtpServer 'smtp.mycompany.com' `
  -Subject 'Files Found in the folder on Server16.' `
  -Body  $("This is a Test mail from server 16 `n This is The file list `n"+`
      ((Get-ChildItem "E:\Test" | ? {! $_.PsIsContainer} | ft -auto name, CreationTime | out-string) -join "`n"))
}

Subsun
when I use this I get an email even if the folder does not have any files in it. Maybe it is because there are folders in the the folder I am checking.

Rajitha14

I keep finding 3 files that do not exist on the production server for some reason. There are not any hidden or system files in the folder. I changed the 0 to 3 and it works fine.
Ok..so...that would result in same output even if you use getfiles() method. So, need to check what those files are. Try running Get-ChildItem E:\Test2 and find the files and check the properties of all those files using fl command. Get-ChildItem E:\Test2 | fl
That's strange..
Get-ChildItem "E:\Test" | ? {! $_.PsIsContainer} will return only files not folders..
Are you getting any result for above command?
Rajitha14

The script is counting the folders on the production server, I can verify this on the test server by having more than one folder in the folder I am monitoring. Very strange why it will ignore one folder but will count the folders if there is more than one.
Subsun

Get-ChildItem "E:\Test" | ? {! $_.PsIsContainer}

Does not return any file names but it counts the folders and sends an email. I do not know why.
Rajitha14

Get-ChildItem E:\Test2 | fl      returns the information on the folders in the folder E:\Test2.
Hmm.. Can you test this by creating another folder?

This is what I get, when testing..
User generated image
Run

Get-ChildItem E:\Test2 | fl

against your folder as suggested. You will get the info on any folders in the folder you are checking. For some reason the scripts are counting the folders that are present.  ONly the files are output but the folders are being counted.
Can you try the following code and see if you get same result?
If((Get-ChildItem "E:\Test" | ? {! $_.PsIsContainer}).Count -gt 0)
{Send-MailMessage -From 'files_on_16@mycompany.com' -To 'me@mycompany.com' `
  -SmtpServer 'smtp.mycompany.com' `
  -Subject 'Files Found in the folder on Server16.' `
  -Body  $("This is a Test mail `n The file list `n"+`
      ((Get-ChildItem "E:\Test" | ? {! $_.PsIsContainer} | ft -auto name, CreationTime | out-string) -join "`n"))
}

Open in new window

Make more than one folder in E:\Test and Run

$fileCount = (Get-ChildItem E:\Test).count
$filecount

You will get a count of your folders. Remove all but one folder and run it again, you will not get a count.
ASKER CERTIFIED SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Neither of the scripts send an email. Doesn't matter if there is a file in the folder or not.
Sorry
Change first line to
If((Get-ChildItem "E:\Test" | ? {! $_.PsIsContainer} | Measure).Count -gt 0)

Open in new window


If it's not working
What is the result for command

(Get-ChildItem "E:\Test" | ? {! $_.PsIsContainer} | Measure).Count
This appears to be working

$fileCount = (Get-ChildItem E:\Test | ? {! $_.PsIsContainer} | Measure).count

I will test some more and get back.
This is working on all servers no matter how many folders are in the folder I am checking.

$fileCount = (Get-ChildItem E:\Test2 | ? {! $_.PsIsContainer} | Measure).count
  If ($fileCount -gt 0)
  {Send-MailMessage -From 'files@mycompany.com' -To 'me@mycompany.com' `
  -SmtpServer 'smtp.mycompany.com' `
  -Subject 'Files Found' `
  -Body  $("The Following Files Were found On `n server 16 Test2 `n" +`
     ((Get-ChildItem "E:\Test2"`
     | ? {! $_.PsIsContainer} | ft -auto name, CreationTime | out-string) -join "`n"))`
}

I hope it continues to work.  Thanks to both of you and special thanks to Subsun for staying with me to get this resolved.