-1

我为交易电子邮件设置了 Azure Runbook 脚本、SMTP 服务器详细信息以及为安全起见编辑的发件人和收件人电子邮件。

出于某种原因,PowerShell 没有看到我想作为附件发送的文件,任何人都可以看到我哪里出错了吗?

我提供了一个屏幕截图来证明我拥有正确的文件(我相信它们被称为“blob”?)名称、存储帐户名称和容器名称

下面是 Runbook PowerShell 脚本,以及测试电子邮件时的错误消息。

一切顺利,

===================RUNBOOK POWERSHELL CODE===========================

$Username ="xxx"

$Password = ConvertTo-SecureString "xxx" -AsPlainText -Force

$credential = New-Object System.Management.Automation.PSCredential $Username, $Password

$SMTPServer = "xxx"

$EmailFrom = "xxx"

[string[]]$EmailTo = "xxx"

$Subject = "xxx"

# Setting the Azure Storage Context,recommedation is to read sastoken from Runbook assets for security purpose.
$context = New-AzureStorageContext -StorageAccountName "expertadvisers" -SasToken "?sv=2019-12-12&ss=bfqt&srt=c&sp=rwdlacupx&se=2021-01-20T17:01:17Z&st=2021-01-20T09:01:17Z&spr=https&sig=PpozvhXnD6k4knwLLpyJvMF0vpnSZATabreYTzr0s2k%3D"

#Get the blob contents from storage container and store it local destination . 
Get-AzureStorageBlob -Container "notificationcontainer" -Blob "test_file.docx" -Context $context | Get-AzureStorageBlobContent -force -Destination .

#Get contents of the file
[string[]] $attachment = "test_file.docx"

$Body = "TEST MESSAGE"
    
Send-MailMessage -smtpServer $SMTPServer -Attachment $attachment -Credential $credential -Usessl -Port 587 -from $EmailFrom -to $EmailTo -subject $Subject -Body $Body -Priority High -DeliveryNotificationOption OnSuccess -BodyAsHtml
Write-Output ("Email sent succesfully.")

===================ERROR MESSAGE===========================

Get-AzureStorageBlob : The remote server returned an error: (403) Forbidden. HTTP Status Code: 403 - HTTP Error 
Message: Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly 
including the signature.
At line:19 char:1
+ Get-AzureStorageBlob -Container "notificationcontainer" -Blob "test_f ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [Get-AzureStorageBlob], StorageException
    + FullyQualifiedErrorId : 
StorageException,Microsoft.WindowsAzure.Commands.Storage.Blob.Cmdlet.GetAzureStorageBlobCommand
 
Send-MailMessage : Could not find file 'C:\Temp\pyxr0xjf.rej\test_file.docx'.
At line:32 char:1
+ Send-MailMessage -smtpServer $SMTPServer -Attachment $attachment -Cre ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Send-MailMessage], FileNotFoundException
    + FullyQualifiedErrorId : System.IO.FileNotFoundException,Microsoft.PowerShell.Commands.SendMailMessage

Azure 存储帐户容器屏幕截图

4

1 回答 1

1

关于这个问题,请参考以下步骤

  1. 通过 protal 创建 SAS 令牌 在此处输入图像描述

  2. 脚本

$Username =""

$Password = ConvertTo-SecureString "" -AsPlainText -Force

$credential = New-Object System.Management.Automation.PSCredential $Username, $Password

$Subject = "test"
$To=""
$Body = "This is an automated mail send from Azure Automation relaying mail using Office 365."
$Context = New-AzureStorageContext -StorageAccountName "andyprivate" -SasToken "<the sas token you created in step1>"
$a=Get-AzureStorageBlobContent -Container "output" -Blob "test.txt" -Context $Context
$attachment = $a.Name
Send-MailMessage `
    -To $To `
    -Subject $Subject  `
    -Body $Body `
    -UseSsl `
    -Port 587 `
    -SmtpServer 'smtp.office365.com' `
    -From $Username `
    -Attachments $attachment `
    -BodyAsHtml `
    -Credential $credential

在此处输入图像描述

于 2021-01-22T05:40:48.963 回答