How can I run an exchange management shell command within a scheduled task? RRS feed

  • Question

  • Hi,

    I am trying to create a Windows Server 2008 R2 scheduled task that launches the exchange management shell application then runs a command. Within the "Action" of the scheduled task I am starting the below program:


    With the arguments:

    -version 2.0 -noexit -command ". 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto" -Command "get-mailcontact -OrganizationalUnit "ABC.local/SP2013 Email Lists" -resultsize:unlimited -filter {ExchangeVersion -eq "0.0 (6.5.6500.0)"}  | Set-MailContact -EmailAddressPolicyEnabled:$true -RequireSenderAuthenticationEnabled:$False -Force"

    But when the task runs I keep getting the below warning:

    Task Scheduler did not launch task "\Microsoft\Windows\SharePoint\Upgrade Exchange Contact"  because instance "{42ef037d-e021-4ace-8a2c-84161175709e}"  of the same task is already running.

    Followed by the below error:

    Task Scheduler failed to start "\Microsoft\Windows\SharePoint\Upgrade Exchange Contact" task for user "ABC\Administrator". Additional Data: Error Value: 2147750687.

    Any thoughts?


    Friday, July 10, 2015 4:24 PM

All replies

  • Thanks for your reply Slava.

    This just adds the arguments anyway and after reviewing test results the command doesn't seem to be executing. I can see from the task history that the task is completing successfully but I don't think the command is actually running? When I open a command prompt on the server and paste the below code in I receive "'Set-MailContact' is not recognised":

    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command “. ‘C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1′; Connect-ExchangeServer -auto; get-mailcontact -OrganizationalUnit "ABC.local/SP2013 Email Lists" -resultsize:unlimited -filter {ExchangeVersion -eq "0.0 (6.5.6500.0)"}  | Set-MailContact -EmailAddressPolicyEnabled:$true -RequireSenderAuthenticationEnabled:$False -Force″

    It seems the only way I can run the command is by opening a command prompt on the server and typing the below:

    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -version 2.0 -noexit -command ". 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto"

    I then have to wait three seconds then type the below:

    get-mailcontact -OrganizationalUnit "ABC.local/SP2013 Email Lists" -resultsize:unlimited -filter {ExchangeVersion -eq "0.0 (6.5.6500.0)"}  | Set-MailContact -EmailAddressPolicyEnabled:$true -RequireSenderAuthenticationEnabled:$False -Force

    This process seems to execute the command. Is it not possible to schedule this process?


    Monday, July 13, 2015 8:36 AM
  • No, the exchange management shell is installed on the server that I am attempting to create the scheduled task on. It seems to be the scheduled task action that I can't get right?

    Monday, July 13, 2015 9:50 AM
  • Your suggestion still doesn't seem to run the code, I still have to manually open exchange powershell on the server then run the below, my schedule task doesn't run it although it is completing successfully?...

    get-mailcontact -OrganizationalUnit "ABC.local/SP2013 Email Lists" -resultsize:unlimited -filter {ExchangeVersion -eq "0.0 (6.5.6500.0)"}  | Set-MailContact -EmailAddressPolicyEnabled:$true -RequireSenderAuthenticationEnabled:$False -Force


    Monday, July 13, 2015 2:53 PM