2013-03-28 132 views
1

我使用ROBOCOPY備份一個硬盤驅動器到另一個下面是我使用的代碼:的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. 在時間戳不同的情況下比較文件時,附加目標文件名並複製源文件。

+0

我已經嘗試了不同的scenerios,我可以打開outlook,但它沒有ttach文件,也沒有發送電子郵件。我相信這是一個安全的前景。至於附加文件沒有嘗試任何東西。 – user1870921 2013-03-28 03:29:56

+3

爲什麼不使用Send-MailMessage CmdLet發送郵件? – JPBlanc 2013-03-28 04:12:27

+0

對於第二點:你的意思是創建一個更改(因此複製)的文件到電子郵件列表? IE瀏覽器。你想變換/重新格式化電子郵件的'robocopy'輸出? – Richard 2013-03-28 09:37:51

回答

2

所以這裏是我用於很多robocopy作業的腳本。我不確定它是否適合你的「通過Outlook發送電子郵件」,因爲它只是在做一個SMTP,它實際上並沒有連接到Outlook。該腳本是Powershell 2.0腳本,並且只記得Server 2008R2和Windows Vista +具有/ MT選項,與/ TEE選項配合使用時效果不佳。腳本在帖子的底部。

對於第二點,我想您可能需要兩個不同的腳本,一個會明確忽略時間戳,另一個會包含時間戳差異文件,如果在第一個之後直接運行,應該是正確的。這將是/ XO開關,它是「/ XO:eXclude較老 - 如果目標文件存在並且與源相同或更新 - 不必重寫它。」

,可以幫助你在腳本的其他選項進行如下:

/XC |/XN:eXclude已更改|較新的文件 /XL:eXclude「孤獨」文件和目錄(存在於源中,但不包含目標)這將防止任何新文件被添加到目標。 /XX:eXclude「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 protected]>" 
$Recipients = "User1 <[email protected]>" 
$Admin = "Admin <[email protected]>" 
$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 
#*=============================================