我想从我的Powershell Runbook脚本发送电子邮件,我想将CSV文件作为脚本输出附加在我的Azure订阅上的存储Blob中。
脚本已准备好,我正在接收电子邮件,但没有附件。我仍然面临这个问题,当我从Azure自动化执行脚本时,作为运行手册,我会看到以下错误消息。我将所有文件存储在同一容器中。
新对象:使用“ 1”参数调用“ .ctor”的异常:“找不到文件 'C:\ Users \ Client \ Temp \ xxxxxxxx.csv'。”
这是我的输出-> $ ArrayStatus | Export-Csv -NoTypeInformation-路径“ $ 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文件附件
答案 0 :(得分:0)
假设您已成功使用Set-AzureStorageBlobContent将export-csv的输出上传到Azure blob存储。您可以使用以下代码片段从存储容器中读取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'
希望这会有所帮助。
答案 1 :(得分:0)
我没有使用“ 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保存文件的本地路径。