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)