Solved

Attach multiple files into the same PowerShell Script Email message

Posted on 2014-02-06
2
3,350 Views
Last Modified: 2014-02-06
Hi Guys,

I am new with Poweshell Script. Pretty new!  :-)
Please be patient!
I found the following code to email me attachments.
However I need $attachment and $attachment2
Currently I am getting only $attachment

How should I change the script below to attach both attachments?


###########Define Variables########
 
$fromaddress = "donotreply@domain.com"
$toaddress = "administrator@domain.com"
$bccaddress = "administrator@domain.com"
$CCaddress = "administrator@domain.com"
$Subject = "Mailbox Statistics Weekly Export"  
$attachment = "C:\scripts\ExportMailboxStats.csv"
$attachment2 = "C:\scripts\LabUsers.csv"
$smtpserver = "smtp.domain.com"
 
####################################
 
$message = new-object System.Net.Mail.MailMessage
$message.From = $fromaddress
$message.To.Add($toaddress)
$message.CC.Add($CCaddress)
$message.Bcc.Add($bccaddress)
$message.IsBodyHtml = $True
$message.Subject = $Subject
$attach = new-object Net.Mail.Attachment($attachment)
$message.Attachments.Add($attach)
$message.body = $body
$smtp = new-object Net.Mail.SmtpClient($smtpserver)
$smtp.Send($message)
 
######################################
0
Comment
Question by:osagarana
2 Comments
 
LVL 28

Accepted Solution

by:
becraig earned 500 total points
ID: 39840845
$message = new-object System.Net.Mail.MailMessage
$message.From = $fromaddress
$message.To.Add($toaddress)
$message.CC.Add($CCaddress)
$message.Bcc.Add($bccaddress)
$message.IsBodyHtml = $True
$message.Subject = $Subject
$message.Attachments.Add($attachment)
$message.Attachments.Add($attachment2)
$message.body = $body
$smtp = new-object Net.Mail.SmtpClient($smtpserver)
$smtp.Send($message)
0
 
LVL 1

Author Closing Comment

by:osagarana
ID: 39840915
thanks mate. It worked.  :-)
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Disabling the Directory Sync Service Account in Office 365 will stop directory synchronization from working.
Marketers need statistics and metrics like everybody else needs oxygen. In this article we explain how to enable marketing campaign statistics for Microsoft Exchange mail.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now