Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell Robocopy发送电子邮件、附件、附加文件名_Powershell_Outlook_Robocopy - Fatal编程技术网

Powershell Robocopy发送电子邮件、附件、附加文件名

Powershell Robocopy发送电子邮件、附件、附加文件名,powershell,outlook,robocopy,Powershell,Outlook,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

我正在使用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
我需要能够做几件事:

  • 通过outlook自动通过电子邮件发送文本文件(powershell?)

  • 比较文件时,如果时间戳不同,请附加目标文件名,然后从源复制文件


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

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

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

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

    #*=============================================
    #*基本变量
    #*=============================================
    $SourceFolder=“C:\SourceFolder”
    $DestinationFolder=“C:\DestinationFolder”
    $Logfile=“C:\Robocopy.log”
    $Subject=“机器人复制结果:复制目的-位置到位置”
    $SMTPServer=“smtp.server.com”
    $Sender=“服务器”
    $Recipients=“User1”
    $Admin=“Admin”
    $sendmail=$True
    $IncludeAdmin=$True
    $AsAttachment=$False
    #*=============================================
    #*GMAIL变量
    #*=============================================
    #$SMTPServer=“smtp.gmail.com”
    #$cred=新对象System.Net.NetworkCredential(“用户名”、“密码”);
    #在发送邮件消息中添加“-UseSsl-Credential$cred”
    #*=============================================
    #*正文
    #*=============================================
    #根据需要更改robocopy选项。( http://ss64.com/nt/robocopy.html )
    机器人复制$SourceFolder$DestinationFolder/MIR/R:2/W:5/LOG:$Logfile/NP/NDL
    #下面尝试获取Robocopy的错误代码
    #并将其用作额外的信息和电子邮件决定。
    #开关和ROBOCOPY命令之间没有其他代码
    交换机($LASTEXITCODE)
    {
    16
    {
    $exit_code=“16”
    $exit_reason=“***致命错误***”
    #$IncludeAdmin=$False
    #$sendmail=$False
    }
    8.
    {
    $exit_code=“8”
    $exit_reason=“**失败的副本**”
    #$IncludeAdmin=$False
    #$sendmail=$False
    }
    4.
    {
    $exit_code=“4”
    $exit_reason=“*不匹配*”
    $IncludeAdmin=$False
    #$sendmail=$False
    }
    2.
    {
    $exit_code=“2”
    $exit\u reason=“额外文件”
    $IncludeAdmin=$False
    #$sendmail=$False
    }
    1.
    {
    $exit_code=“1”
    $exit\u reason=“复制成功”
    $IncludeAdmin=$False
    #$sendmail=$False
    }
    0
    {
    $exit\u code=“0”
    $exit\u reason=“无变化”
    $sendmail=$False
    $IncludeAdmin=$False
    }
    违约
    {
    $exit\u code=“未知($LASTEXITCODE)”
    $exit\u reason=“未知原因”
    #$sendmail=$False
    $IncludeAdmin=$False
    }
    }
    #使用退出原因和退出代码修改主题
    $Subject+=”:“+$exit\u原因+”EC:“+$exit\u代码
    #测试日志文件大小以确定是否应通过电子邮件发送
    #或者只是一封状态邮件
    If((Get ChildItem$Logfile).Length-lt 25mb)
    {
    如果($IncludeAdmin)
    {
    如果($AsAttachment)
    {
    发送邮件消息-从$Sender-到$Recipients-抄送$Admin-主题$Subject-正文“已附加Robocopy结果”-附件$Logfile-发送通知选项失败-SmtpServer$SmtpServer
    }否则{
    发送邮件-从$Sender-到$Recipients-抄送$Admin-主题$Subject-正文(获取内容$LogFile | Out字符串)-传递通知选项onFailure-SmtpServer$SmtpServer
    }
    }否则{
    如果($AsAttachment)
    {
    发送邮件消息-从$Sender-到$Recipients-主题$Subject-正文“已附加机器人复制结果”-附件$Logfile-DeliveryNotificationOptions on Failure-SmtpServer$SmtpServer
    }否则{
    发送邮件消息-从$Sender-到$Recipients-主题$Subject-正文(获取内容$LogFile | Out字符串)-DeliveryNotificationOption on失败-SmtpServer$SmtpServer
    }
    }
    }否则{
    #从$Logfile的开头和结尾创建电子邮件正文
    $Body=“日志文件太大,无法发送。”+(获取内容$Logfile-TotalCount 15 |输出字符串)+(获取内容$Logfile |选择对象-最后13 |输出字符串)
    #如果日志文件太大而无法通过电子邮件发送,则包括管理员
    发送邮件消息-从$Sender-到$Recipients-Cc$Admin-Subject$Subject-Body$Body-DeliveryNotificationOption-onFailure-SmtpServer$SmtpServer
    #如果日志文件太大而无法发送电子邮件,则排除管理员
    #发送邮件消息-从$Sender-到$Recipients-主题$Subject-正文$Body-DeliveryNotificationOption on失败-SmtpServer$SmtpServer
    }
    #*=============================================
    #*脚本结束:Copy-RobocopyAndEmail.ps1
    #*=============================================
    
    我尝试了不同的场景,我可以打开outlook,但它既不附加文件,也不发送电子邮件。我相信这是一个安全功能的前景。就附加文件而言,您尚未尝试任何操作。为什么不使用
    Send-MailMessage
    CmdLet发送邮件?第二点:您的意思是在电子邮件中创建已更改(并因此复制)文件的列表吗?例如,您想转换/重新格式化电子邮件的
    robocopy
    输出吗?
    #*=============================================
    #* 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
    #*=============================================