Email with multiple attachments
Asked Answered
C

1

7

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.

#----- STEP #1 retrieve contents of input file ---#
$INPUTFILEPATH = 'c:\temp\userhomelist.txt'

#----- read input file contents ----#
$inputdata = Get-Content $INPUTFILEPATH

#----- Top section of email body ----#
$msgreport = new-object Net.Mail.MailMessage 
$msgreport = "To view log files, go to directory where this PowerShell Script was run from. `r"
$msgreport = $msgreport + "`r`n"

#read in each username
foreach ($username in $inputdata)
{

#----- STEP #2 robocopy files from \\server to \\nasdevice location ----#
Start-Process -FilePath robocopy -ArgumentList "\\server\userhomes\$username \\nasdevice\userhomes\$username /mir /sec /r:1 /w:1 /tee /NP /Z /log+:userhome-move-$username.log"
$file = "c:\temp\userhome\userhome-move-$username.log"
$msgreport = $msgreport + "$username robocopy has been completed." + "`n"

##----- STEP #3 change file and directory ownership to user ----#
Start-Process -FilePath subinacl -ArgumentList "/subdirectories \\nasdevice\userhomes\$username\*.* /setowner=$username"
$msgreport = $msgreport + "$username file and directory ownership changes have been completed." + "`n"
$msgreport = $msgreport + "`r`n"
}

#----- Email Results ----#
$SmtpClient = new-object system.net.mail.smtpClient
$MailMessage = New-Object system.net.mail.mailmessage 
$SmtpServer = "emailserver.business.com"
$SmtpClient.host = $SmtpServer 
$MailMessage.From = "[email protected]"
$MailMessage.To.add("[email protected]")
$MailMessage.Subject = "User migrations"
$MailMessage.IsBodyHtml = 0
$MailMessage.Body = $msgreport
$MailMessage.Attachments.Add($file)
$SmtpClient.Send($MailMessage)
Caton answered 31/10, 2012 at 21:18 Comment(1)
are you in powershell version 1?Marabout
M
12

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:

$file += "c:\temp\userhome\userhome-move-$username.log"

change $msgreport as [string] type

and then using the send-mailmessage cmdlet do:

send-mailmessage -SmtpServer "emailserver.business.com"  `
-From "[email protected]" -to "[email protected]" `
-Subject "User migrations" -BodyAsHtml -Body $msgreport -Attachments $file
Marabout answered 31/10, 2012 at 21:40 Comment(7)
Sorry, but how do I "change $msgreport as [string] type"?Caton
@Caton just omitting $msgreport = new-object Net.Mail.MailMessage and using $msgreport += ..text here... after the first $msgreport = "To view log files,..... You can and some nr at the end of lines for carriage return formatting...Marabout
So would this email each log separately or one email with all logs?Caton
@Caton actualy your code send one mail with all attachments... for one mail with one log you have to put send-mailmessage in the foreach loop..Marabout
PS C:\temp> .\powershell-v3-email-with-attachment.ps1 Send-MailMessage : The given path's format is not supported. At C:\temp\Userhome-moves.ps1:33 char:17 + Send-MailMessage <<<< -SmtpServer "emailserver.business.com" -From "[email protected]" -to "[email protected]" -Subject "User migrations" -BodyAsHtml -Body $msgreport -Attachments $file + CategoryInfo : NotSpecified: (:) [Send-MailMessage], NotSupportedException + FullyQualifiedErrorId : System.NotSupportedException,Microsoft.PowerShell.Commands.SendMailMessageCaton
@Caton mmh.. try writing like this - -SmtpServer emailserver.business.com without double quote...Marabout
Thanks - this worked for me: $file = @() Get-ChildItem C:\Path\*.* |% { $file += $_.FullName }Illuminative

© 2022 - 2024 — McMap. All rights reserved.