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)
Nick
  • 4,302
  • 2
  • 24
  • 38
rainhailrob
  • 135
  • 1
  • 1
  • 5

1 Answers1

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
CB.
  • 58,865
  • 9
  • 159
  • 159
  • Sorry, but how do I "change $msgreport as [string] type"? – rainhailrob Nov 01 '12 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 `n`r at the end of lines for carriage return formatting... – CB. Nov 01 '12 at 21:54
  • So would this email each log separately or one email with all logs? – rainhailrob Nov 01 '12 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 01 '12 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 – rainhailrob Nov 01 '12 at 22:10
  • @rainhailrob mmh.. try writing like this `- -SmtpServer emailserver.business.com` without double quote... – CB. Nov 01 '12 at 22:24
  • Thanks - this worked for me: `$file = @() Get-ChildItem C:\Path\*.* |% { $file += $_.FullName }` – PeterX Mar 02 '17 at 02:11