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...
You either need to modify your execution policy, or specify the
-ExecutionPolicy Bypass
as a commandline parameter.Not sure if this is how you are doing it, but when I am running PowerShell scripts via task scheduler I use the "Start a program" action and select powershell and then add the arguments from there. That might be what you are doing here, but it's kind of unclear. Here's a screenshot:
Per TheCompWiz execution policy might also be an issue.
I had the same problem. In my case the solution was to specify the "start in" directory (my script would read the contents of a file which it wouldn't find because I didn't specify the full path to the file).
I had the same problem and the comment by "user279399" to use the taskkill command was super helpful, just needed modification. Here's my solution at the end of the argument syntax
This command kills only the powershell session that's running under that user account. It is best practice to have a separate exchange admin account for running scheduled tasks.
Try add to the end of your script [taskkill /f /im "powershell.exe"] this will kill all "powershell.exe" processes. I don't know how to kill the current power-shell process (equivalent to [cmd.exe /c]). But it will do the trick.