【问题标题】:Robocopy sending email, attachment, appending file nameRobocopy 发送电子邮件、附件、附加文件名
【发布时间】:2013-03-18 07:59:47
【问题描述】:

我正在使用 robocopy 将一个硬盘备份到另一个硬盘下面是我正在使用的代码:

robocopy "\\server_location\XXXX" "\\local_location\xxxx" /copyall /TEE /S /r:1 /ndl /xc /xo /xn /log+:"C:\desktop\log.TXT"

START mailto:myemail.job.com?subject=Ovl%%2Reportˆ&body=Test,%%0D%%0A%%0D%%0AHere%%20is%%20is%%the%%20ovl%%20report.%%0D%%0A%%0D%%0ABye

我需要能够做几件事:

  1. 通过 Outlook 自动通过电子邮件发送文本文件(powershell?)。

  2. 比较文件时如果时间戳不同,附加目标文件名,并从源文件复制。

【问题讨论】:

  • 我尝试了不同的场景,我可以打开 Outlook,但它没有附加文件,也没有发送电子邮件。我相信这是前景的安全特征。至于附加文件还没有尝试过任何东西。
  • 你为什么不用Send-MailMessage CmdLet 来发送你的邮件?
  • 第二点:您的意思是在电子邮件中创建更改(并因此复制)文件的列表吗? IE。您想转换/重新格式化电子邮件的robocopy 输出吗?

标签: powershell outlook robocopy


【解决方案1】:

所以这是我在很多 robocopy 工作中都使用过的脚本。我不确定它是否适合您的“通过 Outlook 发送电子邮件”,因为它只是在执行 SMTP,实际上并没有连接到 Outlook 来执行此操作。该脚本是 Powershell 2.0 脚本,记住只有 Server 2008R2 和 Windows Vista+ 有 /MT 选项,与 /TEE 选项一起使用是不好的。脚本在帖子的底部。

对于第二点,我认为您可能需要两个不同的脚本,一个专门忽略时间戳,另一个包含时间戳差异文件,如果直接在第一个之后运行应该是正确的。这将与 /XO 开关一起使用,即“/XO : eXclude Older - 如果目标文件存在并且与源文件的日期相同或更新 - 不要费心去覆盖它。”

其他可能有助于您编写脚本的选项是:

/XC | /XN : 排除已更改 |较新的文件 /XL :排除“孤独”文件和目录(存在于源中但不存在于目标中)这将防止将任何新文件添加到目标。 /XX :排除“eXtra”文件和目录(存在于目标但不存在于源中)这将防止从目标中删除任何内容。 (这是默认设置)

#*=============================================
#* Base variables
#*=============================================

$SourceFolder = "C:\SourceFolder"
$DestinationFolder = "C:\DestinationFolder"
$Logfile = "C:\Robocopy.log"
$Subject = "Robocopy Results: Copy Purpose - Location to Location"
$SMTPServer = "smtp.server.com"
$Sender = "Server <email@address.com>"
$Recipients = "User1 <email@address.com>"
$Admin = "Admin <email@address.com>"
$SendEmail = $True
$IncludeAdmin = $True
$AsAttachment = $False

#*=============================================
#* GMAIL variables
#*=============================================
#$SMTPServer = "smtp.gmail.com"
#$cred = New-Object System.Net.NetworkCredential("username", "password");
# Add "-UseSsl -Credential $cred" to the Send-MailMessage

#*=============================================
#* SCRIPT BODY
#*=============================================

# Change robocopy options as needed. ( http://ss64.com/nt/robocopy.html )
Robocopy $SourceFolder $DestinationFolder /MIR /R:2 /W:5 /LOG:$Logfile /NP /NDL

# The following attempts to get the error code for Robocopy
# and use this as extra infromation and email determination.
# NO OTHER CODE BETWEEN THE SWITCH AND THE ROBOCOPY COMMAND
Switch ($LASTEXITCODE)
{
16
{
$exit_code = "16"
$exit_reason = "***FATAL ERROR***"
#$IncludeAdmin = $False
#$SendEmail = $False
}
8
{
$exit_code = "8"
$exit_reason = "**FAILED COPIES**"
#$IncludeAdmin = $False
#$SendEmail = $False
}
4
{
$exit_code = "4"
$exit_reason = "*MISMATCHES*"
$IncludeAdmin = $False
#$SendEmail = $False
}
2
{
$exit_code = "2"
$exit_reason = "EXTRA FILES"
$IncludeAdmin = $False
#$SendEmail = $False
}
1
{
$exit_code = "1"
$exit_reason = "Copy Successful"
$IncludeAdmin = $False
#$SendEmail = $False
}
0
{
$exit_code = "0"
$exit_reason = "No Change"
$SendEmail = $False
$IncludeAdmin = $False
}
default
{
$exit_code = "Unknown ($LASTEXITCODE)"
$exit_reason = "Unknown Reason"
#$SendEmail = $False
$IncludeAdmin = $False
}
}

# Modify the subject with Exit Reason and Exit Code
$Subject += " : " + $exit_reason + " EC: " + $exit_code

# Test log file size to determine if it should be emailed
# or just a status email
If ((Get-ChildItem $Logfile).Length -lt 25mb)
{
If ($IncludeAdmin)
{
If ($AsAttachment)
{
Send-MailMessage -From $Sender -To $Recipients -Cc $Admin -Subject $Subject -Body "Robocopy results are attached." -Attachment $Logfile -DeliveryNotificationOption onFailure -SmtpServer $SMTPServer
} Else {
Send-MailMessage -From $Sender -To $Recipients -Cc $Admin -Subject $Subject -Body (Get-Content $LogFile | Out-String) -DeliveryNotificationOption onFailure -SmtpServer $SMTPServer
}
} Else {
If ($AsAttachment)
{
Send-MailMessage -From $Sender -To $Recipients -Subject $Subject -Body "Robocopy results are attached." -Attachment $Logfile -DeliveryNotificationOption onFailure -SmtpServer $SMTPServer
} Else {
Send-MailMessage -From $Sender -To $Recipients -Subject $Subject -Body (Get-Content $LogFile | Out-String) -DeliveryNotificationOption onFailure -SmtpServer $SMTPServer
}
}
} Else {
# Creat the email body from the beginning and end of the $Logfile
$Body = "Logfile was too large to send." + (Get-Content $LogFile -TotalCount 15 | Out-String) + (Get-Content $LogFile | Select-Object -Last 13 | Out-String)
# Include Admin if log file was too large to email
Send-MailMessage -From $Sender -To $Recipients -Cc $Admin -Subject $Subject -Body $Body -DeliveryNotificationOption onFailure -SmtpServer $SMTPServer
#Exclude Admin if log file was too large to email
#Send-MailMessage -From $Sender -To $Recipients -Subject $Subject -Body $Body -DeliveryNotificationOption onFailure -SmtpServer $SMTPServer
}

#*=============================================
#* END OF SCRIPT: Copy-RobocopyAndEmail.ps1
#*=============================================

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-14
    • 2016-11-30
    • 2014-10-10
    • 2018-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多