Email with multiple attachments

Question:

I am working on a PowerShell script for the help desk to use when migrating userhome folders from a server to a NAS device. The help desk user enters the usernames into the “userhomelist.txt” file.

My problem is that I’m not able to get the script to attach all of the log files. Only the last log file is attached to the email. I am thinking I need to use a string for multiple attachments, but I keep thinking there is another way.

Answer:

I suggest to use send-mailmessage cmdlet if you are in powershell v2 or v3.
It has an -attachments parameter that accept array of string ( string[] ).

You can change your variable $file declaring it as $file = @() before the foreach user loop.
Inside the foreach do:

change $msgreport as [string] type

and then using the send-mailmessage cmdlet do:

Source:

Email with multiple attachments by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply