Powershell – Exchange PowerShell in a Scheduled Task runs forever

exchangeexchange-2010powershellscheduled-task

I have a couple of Exchange 2010 PowerShell scripts that I'd like to run as Scheduled Tasks.

If I launch PowerShell using "Run as different user" I can run the scripts and they execute correctly.

If I schedule a task using that same user, the task stays in the Running state forever.

How can I figure out where the task is getting stuck?

For reference, here's how I enable the Exchange stuff:

. 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'
Connect-ExchangeServer -auto

And here is the ways I have tried to run the script from the Task Scheduler:

  • powershell -command "& {. 'c:\windows\script.ps1' }"
  • powershell -file 'c:\windows\script.ps1'
  • powershell -file "c:\windows\script.ps1"

All with the same result. Grr…

Best Answer

You either need to modify your execution policy, or specify the -ExecutionPolicy Bypass as a commandline parameter.

powershell -Command "<path to .ps1 script>" -ExecutionPolicy Bypass