none
Task Scheduler PS1 with convertto-html and send-mailmessage doesn't deliver HTML, works in E201SP2RU5 EMS RRS feed

  • Question

  • When I run the script below from EMS it's delivered with the expect HTML table. but if I run it from a task in task scheduler the html is missing.

    I've tried launching it in Task scheduler a couple different  ways with the same results.  This is what I'm using now:

    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command ". 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -ServerFqdn exchange.domain.com; C:\scripts\TESTER.PS1"

    Any ideas?

    =========================

    $a = "<style>"
    $a = $a + "BODY{background-color:White;}"
    $a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
    $a = $a + "TH{border-width: 1px;padding: 5px;border-style: solid;border-color: black;background-color:LightSteelBlue}"
    $a = $a + "TD{border-width: 1px;padding: 5px;border-style: solid;border-color: black;background-color:Linen}"
    $a = $a + "</style>"

    $MBexportBatchRprt = Get-MailboxExportRequest -ResultSize unlimited -BatchName Test-Batch |
        sort name | Get-MailboxExportRequestStatistics |
        select Name,PercentComplete,status,StartTimestamp,CompletionTimestamp,OverallDuration, EstimatedTransferSize, EstimatedTransferItemCount, BadItemsEncountered |
        ConvertTo-Html -Head $a |
        Out-String

    $messageParameters = @{                       
        Subject = "Report 1"                       
        Body = "Contact LH1@domain.com with problems. <br/><br/> $MBexportBatchRprt"
        From = "LH1@domain.com"                       
        To = "MB1@domain.com"                       
        SmtpServer = "exchange.domain.com"                       
        }                       
       
    Send-MailMessage @messageParameters -BodyAsHtml 

    =========================

    Friday, April 24, 2015 12:54 PM

Answers

  • Use powershell.exe as the executable and use its -File parameter to run the script (rather than -Command).


    -- Bill Stewart [Bill_Stewart]

    Thursday, May 7, 2015 12:52 PM
    Moderator

All replies

  • I ended up  putting the  ps1 command in a  batch  file and scheduling it with task scheduler.  That works for  some reason.
    Thursday, May 7, 2015 1:26 AM
  • Use powershell.exe as the executable and use its -File parameter to run the script (rather than -Command).


    -- Bill Stewart [Bill_Stewart]

    Thursday, May 7, 2015 12:52 PM
    Moderator
  • Just an FYI... The -File switch didn't work for me either.  For some reason I had a very difficult time get the command syntax correct, or maybe it didn't work initially for some reason.  The following ended up working:

    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command ". 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -ServerFqdn server1.domain.com; D:\scripts\Script1.ps1"

    Tuesday, June 23, 2015 3:50 PM
  • It doesn't have to be that difficult. Just put your 3 commands into a single script:


    . "C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1"
    Connect-ExchangeServer -ServerFqdn server1.domain.com
    D:\scripts\Script1.ps1
    

    Then schedule the script (with the 3 commands in it) using the -File parameter.

    -- Bill Stewart [Bill_Stewart]


    Tuesday, June 23, 2015 3:57 PM
    Moderator
  • Once I found the HTML wasn't coming through in the messages, I was just going according to an MS blog, and some other articles trying to find a way that it would work.  There are obviously multiple ways to do it, and I have other scripts scheduled without problem, and this one worked except for the HTML part.  I tried multiple iterations including with the -File switch that didn't work.  The one I ended up with did, and I really don't know what was happening.  It's a Windows 2008 SP2 box and there was a suggestion of a bug, but I wasn't able to find any detail on that.  So, this is how it ended up.  I appreciate your help!

    Tuesday, June 23, 2015 7:09 PM