none
Executing powershell script via task scheduler RRS feed

  • Question

  • Hello,

    I am trying to execute powershell script via windows task scheduler which will forcefully delete all email in a mailbox, but I am getting the below errors

    Operational Code - 2
    Task Category - Action Completed --- Task Scheduler successfully completed task "\IL98" , instance "{2b193375-cbd2-4986-918d-3cfd4027d6b6}" , action "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" with return code 0.
    Last Run Result - 0X0

    When I am adding additional cmdlets in the script "Set-ExecutionPolicy Unrestricted", I am getting Last Run Result 0X5 rest of the error remains the same.

    Powershell version - 3.0
    Task Scheduler version - 1.0

    If I try to run the script manually via Exchange Powershell the script executes without any errors

    Thanks in Advance,
    Ankush Shah

    Monday, April 28, 2014 2:35 PM

Answers

  • I figured out the problem, there wasn't any problem with the script but I had to correct inputs in windows task scheuler

    Below is the resolution:

    Under Actions:
        Program/Script --> ws\System32\WindowsPowerShell\v1.0\powershell.exe
        Add Argument --> -noexit -command ". 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto ; <filepath>"

    Eariler I was writing the filepath after double quote "

    Also I have selected the option "Run with Highest Privilages" to avoid 0x5 error

    • Marked as answer by Ankush Shah Saturday, May 3, 2014 6:34 AM
    Saturday, May 3, 2014 6:33 AM

All replies

  • Could you post your script?

    That way we can reproduce the error.

    Monday, April 28, 2014 2:38 PM
  • Access denied means you do not have sufficient authority to set the execution policy.  You must run elevated to set policy.  YOu cannot set it if Group Policy has restricted this.

    0x5 or most extended error coed with a 5 indictate "access denied"


    ¯\_(ツ)_/¯

    • Proposed as answer by jrv Saturday, May 3, 2014 8:15 AM
    Monday, April 28, 2014 2:48 PM
  • Script:

    Start-Transcript -Path C:\TEMP\trans.txt
    Set-ExecutionPolicy Unrestricted
    Add-PSSnapin Microsoft.Exchange.Management.Powershell.E2010
    $dt = (Get-Date).AddDays(-7).ToShortDateString()
    Search-Mailbox -Identity "NoReply Collishop" -SearchQuery "Received:<$($dt)" -DeleteContent -Force

    Monday, April 28, 2014 3:01 PM
  • I went through this but could not find that instance in Local Policies, can you please provide the exact policy that needs to be tweeked
    Monday, April 28, 2014 3:03 PM
  • I went through this but could not find that instance in Local Policies, can you please provide the exact policy that needs to be tweeked

    The PowerShell policies.  If you can set it from the command line then that is not the issue.  The issue is that you need to run elevated.

    Many/most Exchange CmdLets cannot be run under the scheduler.

    You need to isolate which exact command is causing the error.


    ¯\_(ツ)_/¯

    • Proposed as answer by jrv Saturday, May 3, 2014 8:15 AM
    Monday, April 28, 2014 3:21 PM
  • I figured out the problem, there wasn't any problem with the script but I had to correct inputs in windows task scheuler

    Below is the resolution:

    Under Actions:
        Program/Script --> ws\System32\WindowsPowerShell\v1.0\powershell.exe
        Add Argument --> -noexit -command ". 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto ; <filepath>"

    Eariler I was writing the filepath after double quote "

    Also I have selected the option "Run with Highest Privilages" to avoid 0x5 error

    • Marked as answer by Ankush Shah Saturday, May 3, 2014 6:34 AM
    Saturday, May 3, 2014 6:33 AM