jimmylew52
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"))
Original:
Send-MailMessage -From 'files_on_16@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
-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"))
ASKER
Thank You.
This sends the email even if there is not a file in the folder.
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)
ASKER
Yes, The folder with files lists the files, the folder without files does not have a list of files just the text.
ASKER
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"))`
}
$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
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
The code which I posted (ID: 38889616) will send mail only if there is a file in folder..
ASKER
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.
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?
ASKER
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.
{Send-MailMessage -From 'files_on_16@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?
Get-ChildItem "E:\Test" | ? {! $_.PsIsContainer} will return only files not folders..
Are you getting any result for above command?
ASKER
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.
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.
ASKER
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.
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.
ASKER
Rajitha14
Get-ChildItem E:\Test2 | fl returns the information on the folders in the folder E:\Test2.
Get-ChildItem E:\Test2 | fl returns the information on the folders in the folder E:\Test2.
ASKER
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.
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"))
}
ASKER
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.
$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
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
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 it's not working
What is the result for command
(Get-ChildItem "E:\Test" | ? {! $_.PsIsContainer} | Measure).Count
Change first line to
If((Get-ChildItem "E:\Test" | ? {! $_.PsIsContainer} | Measure).Count -gt 0)
If it's not working
What is the result for command
(Get-ChildItem "E:\Test" | ? {! $_.PsIsContainer} | Measure).Count
ASKER
This appears to be working
$fileCount = (Get-ChildItem E:\Test | ? {! $_.PsIsContainer} | Measure).count
I will test some more and get back.
$fileCount = (Get-ChildItem E:\Test | ? {! $_.PsIsContainer} | Measure).count
I will test some more and get back.
ASKER
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.
$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.
Open in new window