Tuesday, April 10, 2012 7:17 PM
I have a powershell script that I wrote that when executed from the shell works fine but when executed from task scheduler does not work.
In my script, an email is sent out based on the results of the execution. When I run this from the shell, email goes out, when scheduled, no email and there is no indication of errors having occurred anywhere in the system.
Has anyone run into a similar issue?
I did change my powershell execution policy to be unrestricted (both in the x86 and x64 consoles). I am running Windows 2008 R2.
Thanks - Greg.
Tuesday, April 10, 2012 7:42 PM
Command Line: powershell -executionpolicy bypass -file script.ps1
Tuesday, April 10, 2012 7:47 PM
Powershell script can be run from Schedule task. Just specify Powershell.exe as application and pass .ps1 (powershell file) as argument.
Secondly, verify following settings for further troubleshoot
1) Make sure you are using appropriate credential on task schedular. Change User Or Group if require.
2) Try to run task with "Run with highest privileges" checked
3) check task history to get more details if task is failing.
Thanks & Regards
Please click “Mark as Answer” if this post answers your question and click "Vote as Helpful if this Post helps you.
- Proposed As Answer by BigteddyMicrosoft Community Contributor Tuesday, April 10, 2012 7:53 PM
Tuesday, April 10, 2012 7:49 PM
In my environment, I run a scheduled job calling a powershell script this way which has worked great:
Under the Actions tab of the scheduled job I have the following information:
Program/Script: C:\windows\system32\windowspowershell\v1.0\powershell.exe (This is always the same)
Add Arguments: <path to the powershell script>
Another thing is to make sure that, if needed, you have Run with highest privileges checked as well.
Please remember to mark the best solution as the answer using Mark as Answer. If you find a solution to be helpful, please use Vote as Helpful.
Looking for a script? Check out the Script Repository
Need a script written for you? Submit a request at the Script Request Page
- Proposed As Answer by BigteddyMicrosoft Community Contributor Tuesday, April 10, 2012 7:52 PM
Wednesday, April 11, 2012 5:43 AMModerator
In addition the above suggestions, please also refer to the below threads:
Using Task Scheduler for a powershell script on server 2008
Using Windows Task Scheduler to execute Powershell Script frequently
Hope this helps.
TechNet Community Support
Wednesday, April 11, 2012 2:40 PM
Thanks for everyone's suggestions, going through my script and the above I've narrowed the problem down to the parameters I am passing in. Some of my parameters have spaces in them and even though they are all enclosed in " ", the task scheduler will only take the first field in the actual parameter.
It's not really an option for me to change the data to not have a space (as I cannot always guarantee this).
-MyUserPolicy "This is the name of the policy"
Anyone have ideas on escaping to get around this?
Wednesday, April 11, 2012 3:22 PM
It can be awkward, difficult, or impossible to pass parameters containing characters that need to be escaped when two or more interfaces must be passed that have different special characters and different escaping methods. My only suggestion would be to experiment.
Another way to accomplish this would be to schedule a different script that runs the script you want to run with the parameters you need to have passed to it.
Wednesday, April 11, 2012 3:44 PM
Found the answer to this - enclosing my entire arguments in double quotes and then my parameters in single quotes ensured that the correct values were being passed to my parameters.
Wednesday, April 11, 2012 3:47 PM
Try wrapping the whole thing up in a cmd or batch file.
Put this in a one line batch file and shedule it
C:\windows\system32\windowspowershell\v1.0\powershell.exe -executionpolicy bypass -file script.ps1 param param param
- Edited by OldDog1 Wednesday, April 11, 2012 3:51 PM spelling
Wednesday, April 17, 2013 9:15 AM
ensure that your task runs with the SYSTEM user account.
Thursday, May 02, 2013 5:36 PM
On a slightly related note, make sure that the execution policy is set to allow the script to run, either on the server itself or from the command called to launch the PowerShell script. You can see your current execution policy setting by running PS:>get-executionpolicy (if it's restricted it won't run).