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

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:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

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?

Thanks.

July 10th, 2015 12:26pm

Put this into program/script field and leave arguments empty:

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

Free Windows Admin Tool Kit Click here and download it now
July 11th, 2015 1:37pm

Put this into program/script field and leave arguments empty:

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

July 11th, 2015 5:34pm

Put this into program/script field and leave arguments empty:

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

Free Windows Admin Tool Kit Click here and download it now
July 11th, 2015 5:34pm

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?

Thanks.

July 13th, 2015 4:38am

I assume that you are trying to run command from server that doesn't have exchange management shell installed. Create this scheduled task directly on exchange server. It may help http://mikepfeiffer.net/2010/02/creating-scheduled-tasks-for-exchange-2010-powershell-scripts/ see also http://www.msexchange.org/kbase/ExchangeServerTips/ExchangeServer2013/Powershell/scheduling-exchange-powershell-task.html
Free Windows Admin Tool Kit Click here and download it now
July 13th, 2015 5:24am

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?

July 13th, 2015 5:52am

From last link for your script:

For Program/script enter:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

Under Add Arguments enter:

-version 2.0 -NonInteractive -WindowStyle Hidden -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 should work.



Free Windows Admin Tool Kit Click here and download it now
July 13th, 2015 6:12am

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

Martin.

July 13th, 2015 10:54am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics