Thursday, August 27, 2020

Send email with multiple attachment using powershell

# This script will take take input value and send email with attachments if matches names with input values
# Load SMO extension
[void][Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
$title = 'Email My Scripts'
$msg = 'Enter your INPUT :'
$INPUTVALUEvers = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title)
$INPUTVALUE = $INPUTVALUEvers -replace '\\','_'

###########Define Variables########
$fromaddress = "xyz@gmail.com"
$toaddress = "xyz@gmail.com"
$Subject = "My report $INPUTVALUEvers"
$body = "My reports"
$smtpserver = "my.com.au"

write-host $INPUTVALUE

[array]$attachments =Get-ChildItem -Path "\\My_path\My\*" -Include 
MyReport_$INPUTVALUE.html,Mylogon_$INPUTVALUE.txt

$Msg = @{
to = $toaddress
from = $fromaddress
Body = $body
subject = $Subject
smtpserver = $smtpserver
BodyAsHtml = $True
Attachments = $attachments.fullname
}
Send-MailMessage @Msg

No comments:

Post a Comment