下面的腳本使用 Outlook 發送帶有 .docx 附件的電子郵件。
我想更改下面的 PowerShell 腳本,以添加它找到的任何 .html 檔案,以及 .docx 檔案。
對修改此腳本的任何幫助表示贊賞。
我想修改這個使用 Outlook 的 PowerShell 電子郵件腳本,以便發送帶有附件的電子郵件。
除了 .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 protected]"
# Iterate over all files and only add the ones that have an .docx extension
$files = Get-ChildItem $FullPath
for ($i=0; $i -lt $files.Count; $i ) {
$outfileName = $files[$i].FullName
$outfileNameExtension = $files[$i].Extension
# if the extension is the one we want, add to attachments
if($outfileNameExtension -eq '.docx')
{
$mail.Attachments.Add($outfileName);
}
}
$mail.Send()
# give time to send the email
Start-Sleep 5
# quit Outlook
$o.Quit()
#end the script
#exit
}
uj5u.com熱心網友回復:
目前尚不清楚是否也應附加任何子目錄中的檔案,但以下是兩種情況的代碼:
場景 1:僅來自 的檔案$FullPath,而不來自任何子目錄
# Iterate over all files and only add the ones that have a .docx or .html extension
$files = Get-ChildItem -Path $FullPath -File | Where-Object { '.docx', '.html' -contains $_.Extension }
foreach ($file in $files) {
$mail.Attachments.Add($file.FullName);
}
$FullPath場景 2:及其子目錄中的所有 .docx 和 .html 檔案
# Iterate over all files and only add the ones that have a .docx or .html extension
$files = Get-ChildItem -Path $FullPath -File -Recurse -Include '*.docx', '*.html'
foreach ($file in $files) {
$mail.Attachments.Add($file.FullName);
}
之后,您可以發送電子郵件。
注意要停止消耗記憶體,您應該釋放 COM 物件:
$o.Quit()
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($mail)
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($o)
$null = [System.GC]::Collect()
$null = [System.GC]::WaitForPendingFinalizers()
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/524872.html
