#1 11 Aug 2011 21:45

cgman
Member
Registered: 16 Feb 2009
Posts: 47

Powershell and multiple attachments

Hi Folks,

I am trying to send multiple files with powershell
this works perfect with one file, but since i have multiple txt files, it fails.

$SMTPserver = "127.0.0.1"

$AllLogs = "C:\choose\your\directory\*.txt"

send-mailmessage -SmtpServer $SMTPserver -to "name@domain.com" -from "name@domain.com" -attachments "$AllLogs" -subject "test mail from powershell" -body "This email was built in powershell and adds multiple attachments";


What am i missing to have the ability to use the -attachment(s) function properly?

Thanks,

Cgman

Offline

#2 11 Aug 2011 22:52

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 1,018
Website

Re: Powershell and multiple attachments

If you look through the help for send-mailmessage:

-Attachments
  The path and file names of files to be attached to the email message.

^ Those are comma separated FileNames not wildcards or file objects, (which is unusual for PowerShell)

So to add multiple attachments you want a collection of strings, if we pipe the files from GCI into send-mailmessage, adding | foreach {$_.fullname} | into the pipeline will convert the file object into the collection of filenames we need.

$SMTP = "127.0.0.1"

Get-ChildItem "C:\choose\your\directory\" -Include *.txt | Where {-NOT $_.PSIsContainer} | foreach {$_.fullname} | Send-MailMessage -from "name@domain.com" -to "name@domain.com" -subject "test mail from powershell" -smtpServer $SMTP

Offline

#3 12 Aug 2011 06:59

cgman
Member
Registered: 16 Feb 2009
Posts: 47

Re: Powershell and multiple attachments

This somewhat works,

I modified this for get-item and used a for each item in the get-item  send the attachment


Foreach ($item in $Files)
{
Send-MailMessage -from "name@domain.com" -to "name@domain.com" -subject "test mail from powershell" -smtpServer $SMTPserver -attachments $item;

}

With yours or mine i get a message

Send-MailMessage : Unable to write data to the transport connection: An established connection was aborted by the software in your host machine.

The kicker is, the for each processes for all of the log.txt files with this error, but I STILL RECEIVE THE EMAIL WITH THE CORRECT LOG FILE.  i am baffled as to why the error occurs, but i am very happy the files attach. 

Thank for your help!!

Offline

#4 12 Aug 2011 10:33

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 1,018
Website

Re: Powershell and multiple attachments

Most likely that error is caused by a firewall or antivirus software thats configured to only allow your email program to send mail, not PowerShell.

Offline

#5 13 Aug 2011 03:26

cgman
Member
Registered: 16 Feb 2009
Posts: 47

Re: Powershell and multiple attachments

I found the issue.  the SMTP server was set to a low connections allowed number i have quite a few files and some of them were over the size limit and they were all being sent at once.  I will post the code that works shortly.

Thanks for all of the help.

Offline

#6 29 Aug 2011 16:39

cgman
Member
Registered: 16 Feb 2009
Posts: 47

Re: Powershell and multiple attachments

Better late than never.

It's not 100% perfect, but this is what I used to send multiple files.
an email is sent for each file, as PS is being stubborn right now, and
each email has the same list of files which i need to break out.


Foreach ($item in $Files)
{
Send-MailMessage -from "domain.com" -to $recipients -subject "Daily Productions Server Logs $FileName" -smtpServer $SMTPserver -attachments $item -body "anything goes here.";

}

Thanks for your help

Offline

Board footer

Powered by FluxBB