none
Executing powershell scripts via Task Scheduler

    Question

  • Hi,

    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:17 PM

Answers

All replies

  • How to Schedule a PowerShell Script   

    Use Scheduled Tasks to Run PowerShell Commands on Windows

    Command Line: powershell -executionpolicy bypass -file script.ps1

        

    • Edited by KazunMVP Tuesday, April 10, 2012 7:49 PM
    • Proposed as answer by Bigteddy Tuesday, April 10, 2012 7:55 PM
    Tuesday, April 10, 2012 7:42 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
    Bhavik Solanki

    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 Bigteddy Tuesday, April 10, 2012 7:53 PM
    Tuesday, April 10, 2012 7:47 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.


    Boe Prox

    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 Bigteddy Tuesday, April 10, 2012 7:52 PM
    Tuesday, April 10, 2012 7:49 PM
  • Hi,

    In addition the above suggestions, please also refer to the below threads:

    Using Task Scheduler for a powershell script on server 2008

    http://social.technet.microsoft.com/Forums/en-US/ITCG/thread/e298d613-47b8-4492-92d1-0b55cc8497c1

     Using Windows Task Scheduler to execute Powershell Script frequently

    http://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/5901a6ad-ba18-4817-82a9-f75d2d6b439f

    Hope this helps.

    Best Regards,

    Yan Li


    Yan Li

    TechNet Community Support

    Wednesday, April 11, 2012 5:43 AM
  • Hi,

    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).

    I.e., 

    -MyUserPolicy "This is the name of the policy"

    Anyone have ideas on escaping to get around this?

    Greg

    Wednesday, April 11, 2012 2:40 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.


    Al Dunbar

    Wednesday, April 11, 2012 3:22 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.

    http://www.techtalkz.com/microsoft-windows-powershell/146575-commandline-argument-parsing-spaces.html

    Wednesday, April 11, 2012 3:44 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 11, 2012 3:47 PM
  • Hi Greg,

    ensure that your task runs with the SYSTEM user account.

    Best Regards,

    Lars

    Wednesday, April 17, 2013 9:15 AM
  • 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).

    Thursday, May 02, 2013 5:36 PM