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

Result of Powershell Script sent via email

根据 运行 下面脚本的结果,returns 如下:

如何通过电子邮件发送此结果?我不确定如何将结果放入可以传递到电子邮件脚本正文中的参数中:

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

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

$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