Powershell and multiple attachments
Posted: 2021-Jul-25, 10:54 am
11 Aug 2011 21:45
cgman
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.
What am i missing to have the ability to use the -attachment(s) function properly?
Thanks,
Cgman
----------------------------
#11 Aug 2011 22:52
Simon Sheppard
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.
----------------------------
#12 Aug 2011 06:59
cgman
This somewhat works,
I modified this for get-item and used a for each item in the get-item send the attachment
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!!
----------------------------
#12 Aug 2011 10:33
Simon Sheppard
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.
----------------------------
#13 Aug 2011 03:26
cgman
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.
----------------------------
#29 Aug 2011 16:39
cgman
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.
Thanks for your help
cgman
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.
Code: Select all
$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";
Thanks,
Cgman
----------------------------
#11 Aug 2011 22:52
Simon Sheppard
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.
Code: Select all
$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
#12 Aug 2011 06:59
cgman
This somewhat works,
I modified this for get-item and used a for each item in the get-item send the attachment
Code: Select all
Foreach ($item in $Files)
{
Send-MailMessage -from "name@domain.com" -to "name@domain.com" -subject "test mail from powershell" -smtpServer $SMTPserver -attachments $item;
}
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!!
----------------------------
#12 Aug 2011 10:33
Simon Sheppard
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.
----------------------------
#13 Aug 2011 03:26
cgman
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.
----------------------------
#29 Aug 2011 16:39
cgman
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.
Code: Select all
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.";
}