通过电子邮件发送Powershell脚本的结果

时间:2019-06-30 01:18:53

标签: powershell azcopy

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

enter image description here

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

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

1 个答案:

答案 0 :(得分:3)

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

$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