我想从我的powershell runbook脚本发送一封电子邮件,我喜欢将csv文件附加为脚本输出,该脚本输出存储在我的Azure订阅中的存储blob中。
脚本已经准备就绪,我正在收到电子邮件,但没有附件。我仍然面临问题,当我从Azure Automation执行脚本时,作为运行手册,我可以看到以下错误消息。我将所有文件存储在同一容器中。
new-object:异常调用" .ctor"带有" 1"参数:"找不到文件'c: users client temp xxxxxxxxxxxxxxxx'。"
这是我的输出 -> $ arraystatus |Export -CSV -NotyPeinFormation -Path" $ filename"
$ filename是一个CSV文件位置
$ArrayStatus | Export-Csv -NoTypeInformation -Path "$filename"
Get-AzureStorageBlob -Container "xxx" -Blob "$filename" -Context $Context
Get-AzureStorageBlobContent -force
$attachment = "$filename"
Function Email ($EmailTo, $Body){
$EmailFrom = "xxx"
$Subject = "xxx Alert"
$SMTPServer = "xxx"
$SMTPMessage = New-Object
System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
$attach = New-Object System.Net.Mail.Attachment($attachment)
$SMTPMessage.Attachments.Add($attach)
$SMTPMessage.IsBodyHTML = $false
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, xx)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("xx",
"xx");
$SMTPClient.Send($SMTPMessage)
}
我正在接收电子邮件,但没有CSV文件附件
,只要您已成功地将导出CSV的输出上传到Azure Blob存储中,并成功地将其上传到了Azure Blob存储中。您可以使用以下片段从存储容器中读取斑点,并将其存储到本地目的地并获取文件的内容并发送电子邮件作为附件。
# Setting the Azure Storage Context,recommedation is to read sastoken from Runbook assets for security purpose.
$context = New-AzureStorageContext -StorageAccountName "storageaccountname" -SasToken "your storage account sastoken"
# Get the blob contents from storage container and store it local destination .
Get-AzureStorageBlob -Container "containername" -Blob "filename.csv" -Context $context | Get-AzureStorageBlobContent -force -Destination .
#Get contents of the file
$attachment = Get-Content "filename.csv"
#send an email (provided the smtp server is reachable from where ever you are running this script)
Send-MailMessage -From 'user@domain.com' -To 'user@domain.com' -Subject 'Content from Blob Storage' -Body "CSV File from Storage blob, sending an attachment for testing" -Attachments .filename.csv -Priority High -DeliveryNotificationOption OnFailure -SmtpServer 'smtpserver'
希望这会有所帮助。
我没有使用" blob上传和下载"过程。但是我只是将输出文件保存到本地路径中,然后从那里上传
好吧,这是我所做的:
$OutputFileName = "Test.xlsx"
$asOutput | Export-Excel $OutputFileName -AutoSize -AutoFilter -Append
Send-MailMessage -To $reciever -from $sender -Attachments ".Test.xlsx" -Subject 'Non-Compliance Report of subscription' -Body "PFA" -smtpserver smtp.office365.com -usessl -Credential $credemail -Port 587
我只是使用"."
进入本地路径,该路径从输出Excel保存。