电子邮件与多个附件 [英] Email with multiple attachments

查看:154
本文介绍了电子邮件与多个附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PowerShell脚本来帮助桌面将用户文件夹从服务器迁移到NAS设备时使用。帮助台用户将用户名输入到userhomelist.txt文件中。



我的问题是我无法让脚本附加所有的日志文件。只有最后一个日志文件附加到电子邮件。我想我需要使用多个附件的字符串,但我一直认为还有另一种方式。

 #--- - 步骤1检索输入文件的内容---#
$ INPUTFILEPATH ='c:\temp\userhomelist.txt'

#-----读入输入文件内容----#
$ inputdata = Get-Content $ INPUTFILEPATH

#-----电子邮件主体的顶部----#
$ msgreport = new -object Net.Mail.MailMessage
$ msgreport =要查看日志文件,请转到此PowerShell脚本运行的目录。`r
$ msgreport = $ msgreport +`r`n

每个用户名中的#read
foreach($ inputdata中的$ username)
{

#----- STEP#2 robocopy files from \\server to \\\\
asdevice location ----#
Start-Process -FilePath robocopy -ArgumentList\\server\userhomes\ $ username \\\\
asdevice\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已经完成。 +`n

## ----- STEP#3将文件和目录所有权更改为用户----#
Start-Process -FilePath subinacl -ArgumentList/子目录\\\\
asdevice\userhomes\ $ username\ *。* / setowner = $ username
$ msgreport = $ msgreport +$ username文件和目录所有权更改已经完成。 +`n
$ msgreport = $ msgreport +`r`n
}

#-----电子邮件结果----#
$ 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 =用户迁移
$ MailMessage.IsBodyHtml = 0
$ MailMessage.Body = $ msgreport
$ MailMessage.Attachments.Add($ file)
$ SmtpClient.Send $ MailMessage)


解决方案

我建议使用 send-mailmessage cmdlet如果你在PowerShell v2或v3中。
它有一个接受数组( string [] )的 -attachments 参数。 >

您可以将变量 $ file 声明为 $ file = @() foreach 用户循环之前。
在foreach中执行:

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

更改 $ msgreport as [string] type



然后使用 send-mailmessage cmdlet do:

  send-mailmessage -SmtpServeremailserver.business.com`
- 从userhome-migration@business.com到helpdeskn@business.com`
-Subject用户迁移-BodyAsHtml -Body $ msgreport -Attachments $ file


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)

解决方案

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

这篇关于电子邮件与多个附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆