I have a script which is fetching the latest event log from the remote machine. Send an event log details via outlook to specific group of people. The script is working fine on running through Powershell ISE but not sending email using task scheduler. Any help would be appreciated. Thanks
Script As below:
$Recipients="[email protected]","[email protected]"
Foreach ($name in $Recipients) {
$Outlook = New-Object -ComObject Outlook.Application
$Mail = $Outlook.CreateItem(0)
$Mail.Recipients.Add($name)
$Mail.Subject ="Testing"
$Mail.Body ="Demo"
Write-Host "Sending Email"
$Mail.Send()
}
You should just use Send-MailMessage instead of trying to use the Outlook COM object for this. This way you don't rely on a mail profile and other settings (which need to be set in Outlook under the user which you use to run the task). I also see some weird things in your code : for each recipient you create a new COM Outlook COM object, and then you try to send a mail : moving the For Loop to trigger After the creation of the Outlook COM object seems more logical.