Unable to send mail using powershell with parameters provided to the script

0

For my work I'd like to send an email to our alert-address when a scheduled task fails its execution. I want to do so with a scheduled task executing a powershell script, providing sender & recipient Mail-Address and the other necessary informations for the Powershell-Function Send-MailMessage as parameters.

To achieve this the scheduled Task is configured as follows: Action: Start a programm Program/script: Powershell Add arguments (optional): C:_SUPPORTFILES\PowerShellScripts\MailAlert.ps1 "[SERVERNAME]@[DOMAIN].ch" "alert@[DOMAIN].ch" "NOTFALL:_Scheduled_Task_Failed on Server [SERVERNAME]" "Please Check" "[SMTP-SERVERNAME]"

The mentioned powershell script itself is configured as follows:

Param([string]$FromAddress, [string]$ToAddress, [string]$Subject, [string]$BodyText, [string]$Smtp)
Send-MailMessage -To $ToAddress -Subject $Subject -SmtpServer $Smtp -From $FromAddress -Body $BodyText

Executing the Scheduled Task results in last run result: (0x8007045B)

To identify the problem, I tried to run the command in CMD. The used command is the following:

PS C:\> powershell.exe C:\_SUPPORTFILES\PowerShellScripts\MailAlert.ps1 "[SERVERNAME]@[DOMAIN].ch" "alert@[DOMAIN].ch" "NOTFALL:_Scheduled_Task_Failed on Server [SERVERNAME]" "Please Check" "[SMTP-SERVERNAME]"

Executing this command shows the error message below (german words translated to english):

Send-MailMessage : The remote name could not be resolved: 'Server'
In C:\_SUPPORTFILES\PowerShellScripts\MailAlert.ps1:4 Sign:1
+ Send-MailMessage -To $ToAddress -Subject $Subject -SmtpServer $Smtp -From
$FromA ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~
    + CategoryInfo          : InvalidOperation: (System.Net.Mail.SmtpClient:Sm
   tpClient) [Send-MailMessage], SmtpException
    + FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.Send
   MailMessage

For further investigations, I run the command in the .ps1-File directly from powershell ISE, without using the Information for Send-MailMessage as parameters, but directly utilyzing them in the command. The script shown here could be executed perfectly and the email is beeing delivered.

Send-MailMessage -To "alert@[DOMAIN].ch" -Subject "NOTFALL:_Scheduled_Task_Failed on Server [SERVERNAME]" -SmtpServer "[SMTP-SERVERNAME]" -From "[SERVERNAME]@[DOMAIN].ch" -Body "Please Check"

Another attempt is also working without any problems. Executing the following script is also working and delivering the expected mail. Also executed in powershell ISE.

$FromAddress = "[SERVERNAME]@[DOMAIN].ch"
$ToAddress = "alert@[DOMAIN].ch"
$Subject =  "NOTFALL:_Scheduled_Task_Failed on Server [SERVERNAME]"
$Smtp = "[SMTP-SERVERNAME]"
$BodyText = "Please Check"

#Param([string] $FromAddress, [string] $ToAddress, [string] $Subject, [string] $BodyText, [string] $Smtp)
Send-MailMessage -To $ToAddress -Subject $Subject -SmtpServer $Smtp -From $FromAddress -Body $BodyText

NOTE: All of these attempts were made from the same server provided in the variable [SERVERNAME]

What is the problem, leading to the error message executing the .ps1-File C:_SUPPORTFILES\PowerShellScripts\MailAlert.ps1 providing the informations for Send-MailMessage as parameters?

Any advice highly appreciated. Thanks for help.

powershell
email
scheduled-tasks
asked on Stack Overflow Sep 10, 2020 by Tobi Stalder • edited Sep 10, 2020 by Tobi Stalder

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0