任何人都可以協助獲取目錄中最新的三個檔案以附加到電子郵件嗎?我目前只能使用以下腳本附加一封最新的電子郵件。此外,如果重要,這些電子郵件將始終在檔案名中的某處具有相同的識別關鍵字,例如 1434343Apple20220314、1434343orange20220314、1434343pear20220314
$dir = "C:\Users\Downloads\"
$latest = Get-ChildItem -Path $dir | Sort-Object LastAccessTime -Descending | Select-Object -First 1
$newfile = $latest.name
$FilePath = Join-Path $dir $newfile
$FileExists = test-path $FilePath
If ($FileExists -eq $True) {
$outlook = New-Object -comObject Outlook.Application
$message = $outlook.CreateItem(0)
$message.Recipients.Add([email protected])
$message.Subject = "files"
$message.Body = "this is test email"
$file = $FilePath
$message.Attachments.Add($file)
$message.Display()
Write-Host "File is emailed"
}
else
{write-host "No File Found"}
uj5u.com熱心網友回復:
我真的對 Outlook ComObject 不是很熟悉,但這里有一些快速/骯臟的腳本:
$outlook = New-Object -comObject Outlook.Application
$email = "[email protected]"
$message = $outlook.CreateItem(0)
$message.Recipients.Add($email)
$message.Subject = "files"
$message.Body = "this is test email"
$path = "C:\Users\Downloads"
$lastest = Get-ChildItem -Path $path -File | Sort-Object -Property LastAccessTime -Descending | Select-Object -First 3
foreach ($file in $lastest)
{
$message.Attachments.Add($file.FullName)
}
$message.Display()
真的,您所需要的只是一個回圈,我將它合并到您現有的代碼中。目前,您當前的代碼沒有意義。為什么?
$FilePath如果$latest不包含任何內容,則永遠不會回傳路徑。$FileExists依賴于$FilePath并且正如您從專案符號 1 中看到的那樣,您一Test-Path開始將是無效的。這將使您的if()陳述毫無用處。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/444132.html
標籤:电源外壳
