-1

I am trying to figure out how I can schedule a task to run once a week with task scheduler to copy two files and attach them to an email and email them to a group of people. Is this possible to do? To have a script that will run using power shell grab two files from certain locations and then email them? Would it be similar to what I have below or am I going in the wrong direction?

$ol = New-Object -comObject Outlook.Application 
$message = $ol.CreateItem(0)
$message.Recipients.Add("Deployment")  
$message.Subject = "Website deployment"  
$message.Body = "See attached file"

$file = "K:\Deploy-log.csv, K:\Deploy-log2.csv"
$message.Attachments.Add($file)

Error

Exception calling "Add" with "1" argument(s): "File name or directory name is not valid." At C:\Users\davidb\Desktop\email.ps1:8 char:1 + $message.Attachments.Add($file)

  • FullyQualifiedErrorId : ComMethodTargetInvocation

If I take , K:\Deploy-log2.csv off it will not error but I still do not receive the email either

David Brierton
  • 6,977
  • 12
  • 47
  • 104
  • 2
    There are many many many tutorials on sending emails and attachements with Powershell. Start first with official M$ doc : https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/send-mailmessage?view=powershell-5.1 – Manu Aug 29 '17 at 13:17
  • Just read the link above that contains examples, it shows how to attach files to a mail – Manu Aug 29 '17 at 13:21
  • From Manu P's link regarding the `Send-MailMessage` cmdlet: _`-Attachments` Specifies the path and file names of files to be attached to the email message. You can use this parameter or pipe the paths and file names to Send-MailMessage._ – BenH Aug 29 '17 at 13:23
  • 2
    Possible duplicate of [How to attach a file to an email with PowerShell](https://stackoverflow.com/questions/3997303/how-to-attach-a-file-to-an-email-with-powershell) – BenH Aug 29 '17 at 13:25
  • 2
    Please note the [tag:batch-file] tag does not mean processing multiple files, instead, it is a kind of Windows script. –  Aug 29 '17 at 13:47

1 Answers1

1

In your code, $file is one string which when interpreted as just one path isn't valid. What you are intending is an array of strings, each element of the array is a string with one path. You could loop over the array with either a foreach or a ForEach-Object loop, using the Add() once on each separate path. Would look like this:

$ol = New-Object -comObject Outlook.Application 
$message = $ol.CreateItem(0)
$message.Recipients.Add("Deployment")  
$message.Subject = "Website deployment"  
$message.Body = "See attached file"

$files = "K:\Deploy-log.csv","K:\Deploy-log2.csv"
foreach ($file in $files) {
    $message.Attachments.Add($file)
}
$message.Send()

Alternatively, you could use the Send-MailMessage cmdlet

 Send-MailMessage -From 'Sender <Sender@example.com>' -To "Recipient <Recipient@example.com>" -Subject "Website deployment" -Body "See attached file" -Attachments @("K:\Deploy-log.csv", "K:\Deploy-log2.csv") -SmtpServer smtp.example.com
BenH
  • 9,766
  • 1
  • 22
  • 35
  • It worked how do you send the second one? Just was curious for future reference – David Brierton Aug 29 '17 at 14:54
  • @DavidBrierton No, you don't have to "end" send. What do you mean send the second one? The alternative way to send messages with `Send-MailMessage`? Reasons why it didn't work work likely revolve around your SMTP server. Do that you need credentials (`-Credentials`) because your SMTP server doesn't allow unauthenticated sending? etc. – BenH Aug 29 '17 at 14:58
  • hmm maybe thats what it is then – David Brierton Aug 29 '17 at 15:16
  • Is there a from option with the top version? Obviously the to option is the recipient.add is there an option to say who its from? – David Brierton Aug 29 '17 at 15:22
  • Short Answer: it's messy and one of the many reasons not to use Outlook as a ComObject. Longer Answer: Outlook by default sends as the account from the default profile unless you use the [`SendUsingAccount()` Method](https://msdn.microsoft.com/en-us/vba/outlook-vba/articles/mailitem-sendusingaccount-property-outlook) which you feed `Items` of `$ol.Session.Accounts` – BenH Aug 29 '17 at 15:28
  • Will this work if i run it on a server using task scheduler? – David Brierton Oct 02 '17 at 19:16
  • @DavidBrierton Short answer, yes. Both methods should. But again I recommend not using Outlook as a ComObject because it will have to run as a user, where `Send-Mailmessage` won't have that requirement. – BenH Oct 03 '17 at 13:34