Send-MailMessage-Attachments不接受任何变量为空 [英] Send -MailMessage -Attachments will not accept null for any variables

查看:17
本文介绍了Send-MailMessage-Attachments不接受任何变量为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Powershellv2.0中的Send-MailMessage函数,我使用的是附件的变量,因为并不总是发送附件。如果变量中没有附件(文件位置),我会得到一个错误,否则它会正常工作。如何设置Send-MailMessage函数以输出附件,并在其他时间不会不失败。

Send-MailMessage -BodyAsHtml –From Monitoring@CorporateActions -Priority $Priority –To "steven.meadows@dionglobal.eu" -Attachments $IDCSwiftLogFileAttachment,$SecurityLogFileAttachment, $ClientTypeLogFileAttachment –Subject "Corporate Actions Overnight Processing" –Body "<b><u> Download Status: </u></b> <br><br> $SWIFTDownloadErrorMessage $SecurityDownloadErrorMessage $ClientDownloadErrorMessage $HoldingDownloadErrorMessage $CLISLOOKDownloadErrorMessage $SWIFTDownloadSuccessMessage $SecurityDownloadSuccessMessage $ClientDownloadSuccessMessage $HoldingDownloadSuccessMessage $CLISLOOKDownloadSuccessMessage <b><u> X-Gen Processing Status: </u></b> <br><br> $SWIFTXGenNoInputMessage $SecurityXGenNoInputMessage $ClientXGenNoInputMessage $HoldingXGenNoInputMessage $CLISLOOKXGenNoInputMessage $IDCSwiftXGenSuccessMessage $SecurityXGenSuccessMessage $ClientXgenSuccessMessage $HoldingXgenSuccessMessage $ClientTypeXGenSuccessMessage $IDCSwiftXgenErrorMessage $SecurityXgenErrorMessage $ClientXgenErrorMessage $HoldingXgenErrorMessage $ClientTypeXGenErrorMessage"  –SmtpServer smtp.investmaster.com

推荐答案

您可以使用"Splat"参数:{参数名称,参数值}的散列,以避免在不需要时传递Attachments参数:

$attachments = @()
if ($IDCSwiftLogFileAttachment) {
  $attachments += $IDCSwiftLogFileAttachment
}
# Repeat for each potential parameter

$params = @{}
if ($attachments.Length -gt 0) {
  $params['Attachments'] = $attachments
}

Send-MailMessage @params -BodyAsHtml –From Monitoring@CorporateActions -Priority $Priority # Other parameters

(此使用哈希表传递参数的功能是在PowerShell V2中添加的。)

这篇关于Send-MailMessage-Attachments不接受任何变量为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆