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 = "userhome-migration@business.com"
$MailMessage.To.add("helpdeskn@business.com")
$MailMessage.Subject = "User migrations"
$MailMessage.IsBodyHtml = 0
$MailMessage.Body = $msgreport
$MailMessage.Attachments.Add($file)
$SmtpClient.Send($MailMessage)
1
  • are you in powershell version 1?
    – CB.
    Oct 31, 2012 at 21:30

1 Answer 1

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 "userhome-migration@business.com" -to "helpdeskn@business.com" `
-Subject "User migrations" -BodyAsHtml -Body $msgreport -Attachments $file
7
  • Sorry, but how do I "change $msgreport as [string] type"? Nov 1, 2012 at 21:44
  • @rainhailrob 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...
    – CB.
    Nov 1, 2012 at 21:54
  • So would this email each log separately or one email with all logs? Nov 1, 2012 at 21:54
  • @rainhailrob 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..
    – CB.
    Nov 1, 2012 at 21:55
  • 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 "userhome-migration@business.com" -to "helpdesk@business.com" -Subject "User migrations" -BodyAsHtml -Body $msgreport -Attachments $file + CategoryInfo : NotSpecified: (:) [Send-MailMessage], NotSupportedException + FullyQualifiedErrorId : System.NotSupportedException,Microsoft.PowerShell.Commands.SendMailMessage Nov 1, 2012 at 22:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.