【问题标题】:Result of Powershell Script sent via email通过电子邮件发送的 Powershell 脚本结果
【发布时间】:2019-07-04 11:30:06
【问题描述】:

在运行以下脚本的结果中,它返回以下内容:

如何通过电子邮件发送此结果?我不确定如何将结果转换为可以传递给电子邮件脚本正文的参数:

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/send-mailmessage?view=powershell-6

$azPath = "C:\Program Files (x86)\Microsoft SDKs\Azure\AzCopy\"

Set-Location $azPath

$StorageAccountName = "#"

$StorageAccountKey = "#"

$ContainerName = "#"

$SourceFolder = "#"

$DestURL = "https://$StorageAccountName.blob.core.windows.net/$ContainerName"

$Result = .\AzCopy.exe /source:$SourceFolder /dest:$DestURL /BlobType:block /destkey:$StorageAccountKey /Y /S /XO

$Result

【问题讨论】:

  • 您可以将ConvertTo-Html 用于您的$result,并将其结果用于参数-Body,并提供开关-BodyAsHtml
  • 谢谢@Olaf - 您介意将其作为代码的答案吗?

标签: powershell azcopy


【解决方案1】:

您可以将结果存储在文件中并作为附件发送:

$Result | Out-File Result.txt
Send-MailMessage -From 'User01 <user01@fabrikam.com>' -To 'User02 <user02@fabrikam.com>' -Subject 'Sending the Attachment' -Body "Forgot to send the attachment. Sending now." -Attachments .\Result.txt -SmtpServer 'smtp.fabrikam.com'

或者将$Result(=string[])的内容作为-Body中的字符串发送:

$body = $Result -join '`n' # Join result to a single string with line breaks
Send-MailMessage -From 'User01 <user01@fabrikam.com>' -To 'User02 <user02@fabrikam.com>' -Subject 'Sending the Attachment' -Body $body -SmtpServer 'smtp.fabrikam.com'

或者(如@Olfa 的评论中所述)将其转换为 HTML 并添加 -BodyAsHtml 开关:

$body = $Result | ConvertTo-Html
Send-MailMessage -From 'User01 <user01@fabrikam.com>' -To 'User02 <user02@fabrikam.com>' -Subject 'Sending the Attachment' -Body $body -SmtpServer 'smtp.fabrikam.com' -BodyAsHtml

【讨论】:

  • 错误“表达式或语句中出现意外的令牌'n#'。”与您的第二个选择。可以请教吗?
  • 还得到: CovertTo-Html :术语“CovertTo-Html”未被识别为 cmdlet、函数、脚本文件或可运行程序的名称。
  • 更新了我的答案
  • 谢谢,非常感谢。但是得到以下错误:“Send-MailMessage:无法将'System.Object []'转换为参数'所需的'System.String'类型'不支持指定的方法。”
  • 您使用的是哪个选项?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多