I am trying to write a script which looks in a folder for latest file and email it to a user, after running the below script I am not getting any errors but I am not receiving any email. Not sure how to check for latest file in a folder.
$outlook = New-Object -comObject Outlook.Application
$message = $outlook.CreateItem(0)
$message.Recipients.Add("test@test.com") #obviously this is not the right email
$message.Subject = "test"
$message.Body = "this is test email"
$file = "P:\test\test.csv"
$message.Attachments.Add($file)
Hi I have tried searching on internet and put together a script to list the latest file now i just need to attach the file and send over the email
$dir = "P:\Source\"
$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)
{email bit should come here}
Not sure how to put the send message (i am new to powershell)
All i got this working as below
$dir = "p:\test\"
$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("test@test.com") #obviously this is not the right
email
$message.Subject = "test"
$message.Body = "this is test email"
$file = $FilePath
$message.Attachments.Add($file)
$message.Send()
Write-Host "File is emailed "
}
else
{write-host "No File Found"}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.