[英]Azure Runbook | Sending attachments from my Azure Storage Account in Transactional Emails
[英]Sending attachments via PowerShell from Azure Storage
当我遵循本网站https://adamtheautomator.com/send-mailmessage/的指导时
为什么在我的 Azure 文件资源管理器中显示文件的测试结果 state 无法找到该文件?
下面是代码:
$Username ="<username>"
$Password = ConvertTo-SecureString "<password" -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential $Username, $Password
$SMTPServer = "<server>"
$EmailFrom = "<from email>"
[string[]]$EmailTo = "<to email>"
$Subject = "TEST MESSAGE"
# Setting the Azure Storage Context, recommendation is to read sastoken from Runbook assets for security purpose.
$context = New-AzureStorageContext -StorageAccountName "<ACCOUNT NAME>" -SasToken "<SAS TOKEN>"
# Get contents of the file
$attachment = Get-Content "<filename>"
$Body = "<Body Message>"
Send-MailMessage -smtpServer $SMTPServer -Attachments <file_name> -Credential $credential -Usessl -Port 587 -from $EmailFrom -to $EmailTo -subject $Subject -Body $Body -BodyAsHtml
Write-Output ("Email sent succesfully.")
出现以下错误消息:
Get-Content : Cannot find path 'C:\Temp\4wved2xx.juf\<file name>' because it does not exist.
At line:19 char:15
+ $attachment = Get-Content "<file name>"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\Temp\4wved2xx.juf\<file name>.jpg:String) [Get-Content],
ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand
Send-MailMessage : Cannot validate argument on parameter 'Attachments'. The argument is null or empty. Provide an
argument that is not null or empty, and then try the command again.
At line:30 char:55
+ ... -MailMessage -smtpServer $SMTPServer -Attachments $attachment -Creden ...
+ ~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Send-MailMessage], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.SendMailMessage
有了这个像
$attachment = Get-Content "<filename>"
我怀疑您正在尝试在参数-Attachments
中发送文件的内容,而不是完整路径和文件名本身。
如果是这种情况,只需删除该行并在其中添加完整的文件名:
# suppose your file is `'C:\Temp\4wved2xx.juf\MyFile.txt'`
-Attachments 'C:\Temp\4wved2xx.juf\MyFile.txt'
为避免使用带有大量参数的 cmdlet(如Send-MailMessage
)的长而长的命令行,我建议使用Splatting
$Username ='<username>'
$Password = ConvertTo-SecureString '<password>' -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential $Username, $Password
# set up a hashtable for splatting the parameters to the Send-MailMessage cmdlet
$mailParams = @{
From = 'someone@yourdomain.com'
To = 'someone.else@yourdomain.com'
Subject = 'TEST MESSAGE'
Body = '<Body Message>'
BodyAsHtml = $true
Attachments = 'C:\Temp\4wved2xx.juf\MyFile.txt' # can be multiple files in an array of Full filenames
SmtpServer = 'smtp.yourdomain.com'
Port = 587
Usessl = $true
Credential = $credential
# other parameters go here
}
Send-MailMessage @mailParams
还请注意单引号的使用,这在指定密码时尤其重要,以确保 PowerShell 从字面上理解字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.