在 powershell 中发送带有 Robocopy 摘要的邮件正文
Send mail body with Robocopy Summary in powershell
我正在使用 powershell 从预定任务发送带有 Robocopy 摘要的邮件。
下面是powershell版本的详细信息
Name Value
---- -----
PSVersion 5.1.15063.502
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.15063.502
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
我的脚本在下面,
$pw = Get-Content .\bumblebee.txt | ConvertTo-SecureString
$cred = New-Object System.Management.Automation.PSCredential "xyz\abc", $pw
$cont = gc C:\Users\Vinod_DMSE\Desktop\Bat\backup_log_20170816.log -last 12 |%{"$_ <br/>"}|out-String
$encoding = [System.Text.Encoding]::UTF8
Send-MailMessage -SmtpServer smtp.xyz.com -to "abc@xyc.com" -Credential $cred -from "backupkr.alert@xyz.com" -Subject " Backup Alert" -BodyAsHtml $cont"
这有效,
但是当我收到带有文本对齐问题的邮件正文时。
下面是输出
是否有可能像在 powershell 中那样更对齐地获得输出。
提前致谢,
正如 Ansgar 所暗示的,这个问题是因为 HTML 如何处理空格。 Multiple spaces normally created by the spacebar, the tab key and return key are all ignored when you write code. HTML just interprets them all as whitespace between words, and displays a single space.
所以两种可能的解决方案是使用 <pre>
标记,它代表预格式化文本。在您的脚本上下文中将如下所示:
$cont = "<pre>$(Get-Content C:\Users\Vinod_DMSE\Desktop\Bat\backup_log_20170816.log -last 12 | ForEach-Object {"$_ <br/>"})</pre>"
否则,您可以将常规空格替换为 Non-breaking spaces。
$cont = "$(Get-Content C:\temp\rblog.txt -last 12 | ForEach-Object {"$($_ -replace '\s',' ') <br/>"})"
另一个问题可能是使用字符宽度不均匀的字体。使用带有每个字符宽度相同的字体系列的 <font>
标签将有助于文本更好地对齐。
$cont = "<font face='monospace'>$(Get-Content C:\temp\rblog.txt -last 12 | ForEach-Object {"$($_ -replace '\s',' ') <br/>"})</font>"
经过@BenH 的一些研究和帮助,我完成了我的脚本。
我分享这个是因为我找不到任何直接的解决方案和直接的方式来发送 robocopy 的摘要。这种方法非常直接,任何新手都可以使用。
请看我下面的主脚本,它从服务器备份文件
::Backup Bat file Created By Vinod Amarathunga 2017-08-11 V 3.0 Improved
::Please do not change anything
@echo off
::Login to the server
NET USE \server IP\IPC$ /u:server IP\username password
::Backup
ROBOCOPY "\server IP\c\Program Files (x86)\source" /S E:\Backup\Backup_%date:~-4,4%%date:~-10,2%%date:~-7,2% /unilog+:"E:\Log\backup_log_%date:~-4,4%%date:~-10,2%%date:~-7,2%.log"
::End the session
NET USE \server IP\IPC$ /D
::Archive Backup
"C:\Program Files\WinRAR\WinRAR.exe" a -r E:\TMS_Auto_Backup\Zip\Zip_Backup_%date:~-4,4%%date:~-10,2%%date:~-7,2%.rar E:\TMS_Auto_Backup\Backup\Backup_%date:~-4,4%%date:~-10,2%%date:~-7,2%
forfiles /p E:\Backup /s /m *.* /D -7 /C "cmd /c del @path"
::Call mail script
powershell -ExecutionPolicy ByPass -Command "C:\mail.ps1"
pause
此脚本将删除超过 7 天的文件。
如果你愿意,你可以评论或删除下面的行,
forfiles /p E:\TMS_Auto_Backup\Backup /s /m *.* /D -7 /C "cmd /c del @path"
为了存档备份,我使用了 WinRar。
为了邮寄 robocopy 摘要,我从 powershell 编写了另一个脚本
在主脚本中,下面的行将调用 运行 来发送邮件
powershell -ExecutionPolicy ByPass -Command "C:\mail.ps1"
下面是邮件脚本,
#Poweshell V 1.0 Script created by Vinod Amarathunga V3.0 Improved
$pw = Get-Content .\bumblebee.txt | ConvertTo-SecureString
$cred = New-Object System.Management.Automation.PSCredential "user name", $pw
$date = Get-Date -format "yyyyMMdd"
$cont = "<font face='monospace'>$(Get-Content C:\Bat\backup_log_$date.log -last 12 | ForEach-Object {"$($_ -replace '\s',' ') <br/>"})</font>"
$encoding = [System.Text.Encoding]::UTF8
Send-MailMessage -SmtpServer mail.xyz.com -to "abc@xyz.com" -Credential $cred -from "backup.alert@xyz.com" -Subject "Backup Summary" -BodyAsHtml "$cont"
下一行将有助于识别给定日期的日志,
$cont = "<font face='monospace'>$(Get-Content C:\Bat\backup_log_$date.log -last 12 | ForEach-Object {"$($_ -replace '\s',' ') <br/>"})</font>"
这个日期应该是yyyymmdd格式,这个格式的日期是必须的,
$date = Get-Date -format "yyyyMMdd"
计划任务执行后,我收到以上邮件到我的邮箱,
网上资源比较多,但我觉得这是目前最直接的方法。
要访问我的邮件帐户,需要有用户名和密码。
Powershell 不允许普通密码 link 下面将帮助我了解如何使用加密密码
https://www.pdq.com/blog/secure-password-with-powershell-encrypting-credentials-part-1/
我正在使用 powershell 从预定任务发送带有 Robocopy 摘要的邮件。
下面是powershell版本的详细信息
Name Value
---- -----
PSVersion 5.1.15063.502
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.15063.502
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
我的脚本在下面,
$pw = Get-Content .\bumblebee.txt | ConvertTo-SecureString
$cred = New-Object System.Management.Automation.PSCredential "xyz\abc", $pw
$cont = gc C:\Users\Vinod_DMSE\Desktop\Bat\backup_log_20170816.log -last 12 |%{"$_ <br/>"}|out-String
$encoding = [System.Text.Encoding]::UTF8
Send-MailMessage -SmtpServer smtp.xyz.com -to "abc@xyc.com" -Credential $cred -from "backupkr.alert@xyz.com" -Subject " Backup Alert" -BodyAsHtml $cont"
这有效,
但是当我收到带有文本对齐问题的邮件正文时。
下面是输出
是否有可能像在 powershell 中那样更对齐地获得输出。
提前致谢,
正如 Ansgar 所暗示的,这个问题是因为 HTML 如何处理空格。 Multiple spaces normally created by the spacebar, the tab key and return key are all ignored when you write code. HTML just interprets them all as whitespace between words, and displays a single space.
所以两种可能的解决方案是使用 <pre>
标记,它代表预格式化文本。在您的脚本上下文中将如下所示:
$cont = "<pre>$(Get-Content C:\Users\Vinod_DMSE\Desktop\Bat\backup_log_20170816.log -last 12 | ForEach-Object {"$_ <br/>"})</pre>"
否则,您可以将常规空格替换为 Non-breaking spaces。
$cont = "$(Get-Content C:\temp\rblog.txt -last 12 | ForEach-Object {"$($_ -replace '\s',' ') <br/>"})"
另一个问题可能是使用字符宽度不均匀的字体。使用带有每个字符宽度相同的字体系列的 <font>
标签将有助于文本更好地对齐。
$cont = "<font face='monospace'>$(Get-Content C:\temp\rblog.txt -last 12 | ForEach-Object {"$($_ -replace '\s',' ') <br/>"})</font>"
经过@BenH 的一些研究和帮助,我完成了我的脚本。 我分享这个是因为我找不到任何直接的解决方案和直接的方式来发送 robocopy 的摘要。这种方法非常直接,任何新手都可以使用。
请看我下面的主脚本,它从服务器备份文件
::Backup Bat file Created By Vinod Amarathunga 2017-08-11 V 3.0 Improved
::Please do not change anything
@echo off
::Login to the server
NET USE \server IP\IPC$ /u:server IP\username password
::Backup
ROBOCOPY "\server IP\c\Program Files (x86)\source" /S E:\Backup\Backup_%date:~-4,4%%date:~-10,2%%date:~-7,2% /unilog+:"E:\Log\backup_log_%date:~-4,4%%date:~-10,2%%date:~-7,2%.log"
::End the session
NET USE \server IP\IPC$ /D
::Archive Backup
"C:\Program Files\WinRAR\WinRAR.exe" a -r E:\TMS_Auto_Backup\Zip\Zip_Backup_%date:~-4,4%%date:~-10,2%%date:~-7,2%.rar E:\TMS_Auto_Backup\Backup\Backup_%date:~-4,4%%date:~-10,2%%date:~-7,2%
forfiles /p E:\Backup /s /m *.* /D -7 /C "cmd /c del @path"
::Call mail script
powershell -ExecutionPolicy ByPass -Command "C:\mail.ps1"
pause
此脚本将删除超过 7 天的文件。 如果你愿意,你可以评论或删除下面的行,
forfiles /p E:\TMS_Auto_Backup\Backup /s /m *.* /D -7 /C "cmd /c del @path"
为了存档备份,我使用了 WinRar。
为了邮寄 robocopy 摘要,我从 powershell 编写了另一个脚本 在主脚本中,下面的行将调用 运行 来发送邮件
powershell -ExecutionPolicy ByPass -Command "C:\mail.ps1"
下面是邮件脚本,
#Poweshell V 1.0 Script created by Vinod Amarathunga V3.0 Improved
$pw = Get-Content .\bumblebee.txt | ConvertTo-SecureString
$cred = New-Object System.Management.Automation.PSCredential "user name", $pw
$date = Get-Date -format "yyyyMMdd"
$cont = "<font face='monospace'>$(Get-Content C:\Bat\backup_log_$date.log -last 12 | ForEach-Object {"$($_ -replace '\s',' ') <br/>"})</font>"
$encoding = [System.Text.Encoding]::UTF8
Send-MailMessage -SmtpServer mail.xyz.com -to "abc@xyz.com" -Credential $cred -from "backup.alert@xyz.com" -Subject "Backup Summary" -BodyAsHtml "$cont"
下一行将有助于识别给定日期的日志,
$cont = "<font face='monospace'>$(Get-Content C:\Bat\backup_log_$date.log -last 12 | ForEach-Object {"$($_ -replace '\s',' ') <br/>"})</font>"
这个日期应该是yyyymmdd格式,这个格式的日期是必须的,
$date = Get-Date -format "yyyyMMdd"
计划
网上资源比较多,但我觉得这是目前最直接的方法。
要访问我的邮件帐户,需要有用户名和密码。 Powershell 不允许普通密码 link 下面将帮助我了解如何使用加密密码
https://www.pdq.com/blog/secure-password-with-powershell-encrypting-credentials-part-1/