Powershell script to email any new files in a folder

Hi, I have a need for a Powershell script which will look for any files in a particular folder which:
Are less than a day old;
have a filename that begins with "Micropay View Hours Report"
If any matching files are found, I want the script to email them to people.

By asking Dr Google nicely, I got this script:

Param (
	[string]$Path = "c:\watchfolder\",
	[string]$SMTPServer = "smtp.whatever.com",
	[string]$From = "watchfolder@mycompany.com",
	[string]$To = "myname@mycompany.com;somebodyelse@mycompany.com",
	[string]$Subject = "Hours Worked report for last week"
	)

$SMTPMessage = @{
    To = $To
    From = $From
	Subject = "$Subject at $Path"
    Smtpserver = $SMTPServer
}

$File = Get-ChildItem $Path | Where { ($_.LastWriteTime -ge [datetime]::Now.AddDays(-1) ) -and ( $_.BaseName -like 'Micropay View Hours Report*') }
If ($File)
{	$SMTPBody = "`nThe following files have recently been added/changed:`n`n"
	$File | ForEach { $SMTPBody += "$($_.FullName)`n" }
	Send-MailMessage @SMTPMessage -Body $SMTPBody
	
}

Open in new window


and it works perfectly well BUT it doesn't attach the files to the email. It just lists the filenames as text in the email body.
Unfortunately I know as much about Powershell as a dog knows about his father, and I spent yesterday beating my head against a wall while trying to modify the code so that it would attach the files to the email. I won't even bother pasting the attempts here since they don't work!

So can any of you good people tell me how to modify the code to make it attach the files please?

Thanks for looking!
vistamedAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Barry CunneyCommented:
Hi vistamed,
The Send-MailMessage cmdlet which you are using in your code above has an -Attachments parameter.
So a typical usage would be as follows:
Send-MailMessage @SMTPMessage -Body $SMTPBody -Attachments "sales_data.txt" 

Open in new window


In your case $file should have the list of file attachments so something similar to the following would be the potential usage it your case would be something like the following:
Send-MailMessage @SMTPMessage -Body $SMTPBody -Attachments $file

Open in new window

0
vistamedAuthor Commented:
Hi Barry, that almost works. The only problem is that when it tries to attach the files, the Powershell script is looking in the folder I ran the script from - not the watch folder.

The exact error message is:
.Send-MailMessage : Could not find file 'C:\scriptfolder\Micropay View Hours Report 26 08 2015 to 01 09 2015.pdf'
.

I tried amending the command as follows:
Send-MailMessage @SMTPMessage -Body $SMTPBody -Attachments $Path$file

Open in new window

and that works, but only if there is just one new file.

If there is more than one new file, then the following error occurs:
Send-MailMessage : Could not find file 'C:\watchfolder\Micropay View Hours Report 23 08 2015 to 29 08 2015.xls Micropay View Hours Report 26 08 2015 to 01 09 2015.pdf'.
0
Barry CunneyCommented:
Hi vistamed,
The Attachments parameter of the Send-MailMessage cmdlet can accept a string array - <String[]>
Therefore it should be possible to something like the following:
1. Declare a string array variable named 'file_attachments'
2. Store each full path of each file found in this array
3. Pass this array variable to the Send-MailMessage cmdlet Attachments parameter.

So something like the following:
# Declare an array of type String to store each new file found
$file_attachments = @()

# Iterate through each new file found and store it in the array named $file_attachments
$File | ForEach {$file_attachments += $_.FullName}

# Pass the $file_attachments array to the -Attachments parameter of Send-MailMessage
Send-MailMessage @SMTPMessage -Body $SMTPBody -Attachments $file_attachments

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vistamedAuthor Commented:
Hi Barry,

That did the trick. I love you and I want to have your babies. But to compensate for this, here's 500 points. Thank you very much for your help!

Here's the working code, just in case anyone needs it:

Param (
	[string]$Path = "c:\watchfolder\",
	[string]$SMTPServer = "smtp.wherever.com",
	[string]$From = "watchfolder@mycompany.com",
	[string]$To = "myname@mycompany.com;somebodyelse@mycompany.com",
	[string]$Subject = "Hours Worked report for last week is available at "
	)
$file_attachments = @()

$SMTPMessage = @{
    To = $To
    From = $From
	Subject = "$Subject at $Path"
    Smtpserver = $SMTPServer
}

$File = Get-ChildItem $Path | Where { ($_.LastWriteTime -ge [datetime]::Now.AddDays(-1) ) -and ( $_.BaseName -like 'Micropay View Hours Report*') }
If ($File)
{	$SMTPBody = "`nThe following files have recently been added/changed:`n`n"
	$File | ForEach { $SMTPBody += "$($_.FullName)`n" }
	$File | ForEach {$file_attachments += $_.FullName}

	Send-MailMessage @SMTPMessage -Body $SMTPBody -Attachments $file_attachments
	
}

Open in new window

0
Barry CunneyCommented:
Thankyou, excellent - I am glad this now working for you
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.