Running Powershell Send-mailmessage in Task Scheduler

This topic contains 0 replies, has 1 voice, and was last updated by Profile photo of Forums Archives Forums Archives 5 years, 3 months ago.

  • Author
    Posts
  • #5067

    by bosclarke at 2012-08-31 13:12:23

    I have a powershell script that is running as a task in Task Scheduler in Windows 2008. It runs fine under a service account. The script crunches some logs and sends out a daily report via email to me via our internal Exchange 2007 server using SMTP.

    When I set the task to run under the SYSTEM account, my Exchange server rejects the email because it says the machine account does not have permission to send email.

    On my Exchange server, I have granted the SYSTEM account permission to send SMTP email, so that is not the cause of the problem.

    I have another script. This time just a DOS Batch file. It also sends an email, but uses a SENDMAIL.EXE command line utility.
    When I run this script from the Task Scheduler using the SYSTEM account, it works. The email is not rejected.

    Any ideas as to why Powershell and Send-MailMessage are having this problem?

    by DonJ at 2012-08-31 13:41:38

    The SYSTEM account won't delegate a credential, so even if you assign permissions it won't matter. SendMail is likely doing something explicit with authentication.

    by mjolinor at 2012-08-31 13:56:32

    Instructions on configuring an Exchange 2007 Receive connector for application server relay:
    http://blogs.technet.com/b/exchange/arc ... 97620.aspx

    If you can run the script to send the email from the Exchange server, you can use 'localhost' as your smtp relay, and the connector permissions won't matter.

    by Techibee.Author at 2012-09-03 00:57:53

    It is interesting to know sendmail.exe is working but not the send-mailmessage. What are the arguments you are passing to each of them? Could you please list them here for clarity.

    by bosclarke at 2012-09-04 12:08:41

    The command lines are:

    For the .BAT file:

    REM -s == smtp server
    REM -u == Subject
    REM -m == Message body
    REM -o ==
    sendEmail.exe -s "exchange.afsrealm.com" -u "%Message%" -m "%Message%" -f "%From%" -t "%To%" -cc "%Cc%" -o tls=no

    For the Powershell script:
    send-mailmessage -from $From -to $SendTo -subject $title -body $MessageBody -smtpServer exchange.afsrealm.com

    As for the SYSTEM credentials, until I granted SYSTEM the permission to send SMTP, the .BAT script would also fail with the same error as the Powershell script is doing now.

    by Techibee.Author at 2012-09-05 02:53:29

    [I haven't tried this myself. But this is what I think is the problem]

    AFAIK, some of the cmdlets/pscode doesn't assume current logged on credentials. In such cases we need to explicitly pass the current logged on credentials.

    Change your PS code to something like below and see if works (provided your SYSTEM account still has SMTP permission).

    $cred = [System.Net.CredentialCache]::DefaultNetworkCredentials
    send-mailmessage -from $From -to $SendTo -subject $title -body $MessageBody -smtpServer exchange.afsrealm.com -Credential $cred

    Let me know how it goes.

    by bosclarke at 2012-09-06 12:56:42

    I got the following error when trying your solution:

    Send-MailMessage : Cannot process argument transformation on parameter 'Credential'. userName
    At C:\Server\Tasks\SMTP_Test\PowerShellEmailTest.ps1:24 char:126
    + send-mailmessage -from $From -to $SendTo -subject $title -body $MessageBody -smtpServer exchange.afsrealm.com -Credential < <<< $cred
    + CategoryInfo : InvalidData: (:) [Send-MailMessage], ParameterBindin...mationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Microsoft.PowerShell.Commands.SendMailMessage

    When I look at the $cred variable interactively, I see no user account information listed. From other threads, it seems like one needs to Add a credential to the cache first.

    In desperation, I used Net.Mail.SmtpClient instead:

    $mod1=New-Object Net.Mail.SmtpClient
    $mod1.Host = “exchange.afsrealm.com”
    $mod1.Send($From, $SendTo, $title, $MessageBody)

    That seems to be working just fine!

    by poshoholic at 2012-09-07 05:01:35

    Interesting that Net.Mail.SmtpClient works yet Send-MailMessage does not. I'll share that with some others, see if it's familiar to them.

You must be logged in to reply to this topic.