如何将文件附加到Powershell功能并通过MS Outlook发送?

时间:2016-09-13 12:14:43

标签: email powershell outlook send

我有这段代码:

Function Mailer ($MSubject, $MBody, $File){
$Outlook = New-Object -ComObject Outlook.Application
$Mail = $Outlook.CreateItem(0)
$Mail.To = "abc@contoso.com"
$Mail.Subject = $MSubject
$Mail.Body = $MBody
$Mail.Attachments.Add($File)
$Mail.Send()
}

如果我通过将其分配给$ File变量来提供函数中的确切路径,我将会工作。但是,我希望这对于不同的主题,身体和路径具有普遍性。我应该将文件路径设置为全局吗?你有什么想法?

提前致谢:) 彼得

2 个答案:

答案 0 :(得分:0)

这是我用来发送电子邮件的代码,

$from="ServerAdmin@Contoso.com"
$to="worker@contoso.com"
$cc=@("John.Doe@Contoso.com", "Jane.Doe@Contoso.com")
$subject="Weekly reports"
$attach="c:\SchTsk\Reports\Server_Inventory_$((Get-Date).ToString('MM-dd-yyyy')).csv","C:\SchTsk\Reports\VMPlatform_$((Get-Date).ToString('MM-dd-yyyy')).csv"
$body="This email contains weekly reports ran on the domain.<br>In an effort to reduce inbox spam, reports that generate separate files are now being attached to one weekly email."
Send-MailMessage -from $from -To $to -cc $cc -subject $subject -SmtpServer snmp.relay.contoso.com -Body $body -BodyAsHtml -Attachments $attach

答案 1 :(得分:0)

实际上该功能正常。问题在于我调用的文件路径的变量。