0

我有一个用 PowerShell 编写的 Azure 函数,其输入和输出绑定与存储输入和输出 csv 文件的相同 Blob 存储相同。我能够从该函数发送一封带有 blob 附件的电子邮件,但该电子邮件的附件是类型文件而不是 csv。所以我必须下载并手动添加 .csv 扩展名。如何发送带有附件中扩展名的输出 csv 文件?我的电子邮件代码如下:

# Send Email using SendGrid with file attachment
$username ="Username"
$password = ConvertTo-SecureString "Password" -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential $username, 
$password
$SMTPServer = "smtp.sendgrid.net"
$emailFrom = "No-reply@azureadmin.com"
[string[]]$emailTo = "some-email"
$subject = "Sending sample email using SendGrid Azure and PowerShell"
$body = "This is sample email sent using Sendgrid account create on Microsoft 
Azure. The script written is easy to use."
$attachment = $outputBlob
Send-MailMessage -smtpServer $SMTPServer -Credential $credential -Usessl - 
Port 587 -From $emailFrom -To $emailTo -Subject $subject -Body $body - 
BodyAsHtml -Attachments $attachment

我用相同的代码创建了输出文件,但我不确定如何将其附加到电子邮件中。

outputBlob 是一个名为 outputfile.csv 的 csv 文件,它存储在与输入文件相同的容器中。我在同一个脚本中创建这个输出文件。所以我的容器名称是“mycsvcontainer”,Azure 函数绑定中我的 blob 的路径是“mycsvcontainer/outptfile.csv”,blob 的名称是 outputBlob,我在上面的脚本中使用它。类似的是 inputBlob 的名称。

4

1 回答 1

0
        $StorageAccountName = 'mystorageaccount'
        $StorageAccountKey = "storageaccountkey"
        $StorageContext = New-AzureStorageContext $StorageAccountName -StorageAccountKey $StorageAccountKey
        $FileName = 'filename'
        $ContainerName  = 'mycontainer'

        #Get attachment from blob storage
        $a = Get-AzureStorageBlobContent -Container $ContainerName -Blob $FileName -Context $StorageContext
        $attachment = $a.Name

        $MyCredential = "SendEMail" 
        $EmailBody = "Test"
        $Body = $EmailBody.replace("|","`n`r")
        $subject = "Tets"
        $userid= "Test@test.com"
        $emailTo = "emailid@emaildomain.com"

        #Get the PowerShell credential 
        $Cred = Get-AutomationPSCredential -Name $MyCredential 


        Send-MailMessage `
        -To $emailTo `
        -Subject $subject `
        -Body $Body `
        -UseSsl `
        -Port 587 `
        -SmtpServer 'smtp.sendgrid.net' `
        -Attachments $attachment `
        -From $userid `
        -Credential $Cred `
于 2019-09-08T17:37:59.637 回答