修改PowerShell脚本:让Outlook邮件同时添加.docx与.html附件
修改后的PowerShell脚本(支持添加.docx和.html附件)
以下是调整完成的脚本,已修改附件筛选逻辑,可同时添加指定路径下的.docx和.html文件作为附件:
#SendEMail $SendTo $MailSubject $htmlOutput # Check to see we have all the arguments If (Test-Path -Path "C:\Users\User1\Report\Report.html") { $FullPath=$args[0] #Get an Outlook application object $o = New-Object -com Outlook.Application $mail = $o.CreateItem(0) #2 = High importance message $mail.importance = 1 $mail.subject = "Report: $(get-date)" $mail.HTMLBody = "Report $(get-date)`n$(Get-Content 'C:\Users\User1\Report\Report.html' | Out-String)" $mail.To = "email@emaildomain.com" # Iterate over all files and add .docx and .html extensions $files = Get-ChildItem $FullPath for ($i=0; $i -lt $files.Count; $i++) { $outfileName = $files[$i].FullName $outfileNameExtension = $files[$i].Extension # 筛选.docx和.html格式的文件添加为附件 if($outfileNameExtension -eq '.docx' -or $outfileNameExtension -eq '.html') { $mail.Attachments.Add($outfileName); } } $mail.Send() # give time to send the email Start-Sleep 5 # quit Outlook $o.Quit() #end the script #exit }
核心修改说明
- 扩展了附件筛选的
if判断条件,通过-or运算符同时匹配.docx和.html两种扩展名,确保两类文件都能被加入附件。 - 也可以用更简洁的数组包含判断实现相同效果,替换原
if条件即可:if(@('.docx', '.html') -contains $outfileNameExtension)
内容的提问来源于stack exchange,提问作者ecuso
相关产品推荐
相关产品推荐

