Powershell commands run in an interactive window but not thru Windows Scheduler RRS feed

  • Question

  • I have the following powershell commnad

    Send-MailMessage -From <my email>  -To <my email> -Subject "subject with  some-word-with-hyphens" -SmtpServer <smtp server name> -Attachment <filename>  

    This command works interactively in powershell window. When I schedule it with Windows task scheduler the following things happen.

    1. It gives error message saying positional parameter not found for the hyphenated word.

    2. If the hyphenated word is removed then the email is not sent.

    3. I cannot redirect errors to a log file by using 2>&1 and there is no way to capture error output.

    Thanks for any help on this one.

    Wednesday, September 13, 2017 7:49 PM

All replies

  • You cannot use a CmdLet as a task.  Place your script in a file and use PowerShell as the task executable. For the arguments use:

    Arguments: -file <path>\yourfile.ps1


    Wednesday, September 13, 2017 8:03 PM
  • Unfortunately the ability to execute powershell scripts is revoked on my work computer where there is this issue.

    So using a powershell script is not an option.

    Thursday, September 14, 2017 5:25 PM
  • If PowerShell execution is blocked then there is no answer for your question.  

    I suspect that the default settings are still in effect which is estricted.

    Run PowerShell:

    powershell -bypass -file myscript.psq


    Thursday, September 14, 2017 5:28 PM