none
Using Task Scheduler for a powershell script on server 2008 RRS feed

  • Question

  • I have a powershell script I am trying to run as a scheduled task, but I cannot get it to run. if I run in from within powershell or in an elivated command prompt it runs fine. Task Scheduler says it completes, but nothing happens. Here is the action I am using in Task Scheduler:

    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noninteractive -nologo -command "& {d:\backup\createVM.ps1}"

    I do have powershell set to "allsigned" and my script is signed. (it was a pain to do , but I decided the added security was worth it)
    I have also tried running it as System and the local admin, but that does not seem to matter.
    Friday, May 8, 2009 7:06 PM

Answers

  • It looks like I finally have a solution.
    In PowerShell I set the execution policy from AllSigned to RemoteSigned and then the script would run from Task Scheduler. What I still do not understand is if my code was not signed correctly it would not run in PowerShell at all. I tested this to verify. So if my code is signed and PowerShell is set to AllSigned the script does not run in via Task Scheduler, but does not manually.

    I was attempting to be a good steward of security and keep it AllSigned, but that does not look like it works. So I have set my servers to RemoteSigned. If anyone else has tested this let me know, as I would be interested to hear what others have found.

    Thanks for all the help.

    Nua-O
    • Marked as answer by Nua-O Wednesday, July 1, 2009 12:37 PM
    Wednesday, July 1, 2009 12:37 PM

All replies

  • Use this command:

    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noninteractive -nologo d:\backup\createVM.ps1
    Saturday, May 9, 2009 12:53 AM
    Moderator
  • That did not work. From what I have been reading it appears that with server 2008 it requires a "& {c:\myscript.ps1}" format to run, but then again I cannot get it to work.  I have tried with and without quotes and with and without braces. I have also tried a multitude of different arguments, but nothing has worked. Task Scheduler says it completes fine. Also just to check my script again I ran this from a command prompt:

    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command "& {d:\backup\createVM.ps1}"

    And this runs fine from there, just not as a scheduled task. This should be so simple!?!?
    Thursday, May 14, 2009 8:20 PM
  • This should work. There is no need for & and {}. 

    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noninteractive -command d:\backup\createVM.ps1

    If it does not work... what is the error? Is there an error?
    Brandon Shell [MVP]
    Thursday, May 14, 2009 8:52 PM
    Moderator
  • This is how I do it:

    In the Program/script, put

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

    In the Add arguments (optional) -noninteractive -nologo d:\backup\createVM.ps1

    Works great for me.

    Karl
    • Proposed as answer by Poleza Thursday, December 5, 2013 11:07 AM
    Monday, May 18, 2009 5:03 PM
  • Karl,

    What you wrote works fine from a command prompt, but Task Scheduler will not run it. Rather Task Scheduler history says, in this order:
    Task Started
    Action started
    Created Task Process
    Action completed
    Task completed

    Looking at the time stamp it takes all of 1 second for this to happen. When I check the virtual machines, no checkpoints have been created. However, I take this same command string and put it in command prompt and run it and it all works. There are no errors in Task Manager or Event log, they both say everything is fine. It seems to me that Task Manager does not like something with the arguments. Meaning it runs the task of launching the powershell.exe, but then ignores the arguments after that.

    Thanks
    Friday, June 5, 2009 6:52 PM
  • Does the account you used in schedular have permission to run the script?
    Friday, June 5, 2009 9:10 PM
  • I have tried using both System and the local admin to try to run the script.
    Tuesday, June 9, 2009 5:03 PM
  • Change the account to a domain account that you have verified can login to the machine and run the script from powershell (NOT in an elevated shell).

    Karl

    Tuesday, June 9, 2009 5:18 PM
  • I tried with the domain account, which has local admin privileges, and the script will not run. I get the following error:

    You cannot call a method on a null-valued expression.
    At D:\backup\createVM.ps1:11 char:41
    +     $VM_service.CreateVirtualSystemSnapShot( <<<< $VM.__PATH)

    The task in Task Scheduler is running with local admin privileges not my domain account.
    Thursday, June 11, 2009 3:45 PM
  • Sounds like a permissions issue, assuming it still runs fine in an elevated shell.
    Thursday, June 11, 2009 7:02 PM
  • How did you end up in this group and why did you choose to piggyback this particular thread?
    Windows 2008 scripting hasn't got much in common with Windows 98se problems.

    My suggestion is that you revert to Vista and if you need any assistance with that, try one of these groups:
    http://social.answers.microsoft.com/Forums/en-US/category/windowsvista/
    And hey, if you do ask for help there - start a new thread instead of just picking a thread about something completely different. I'm sure they will appreciate that.
    Monday, June 15, 2009 8:33 PM
  • It looks like I finally have a solution.
    In PowerShell I set the execution policy from AllSigned to RemoteSigned and then the script would run from Task Scheduler. What I still do not understand is if my code was not signed correctly it would not run in PowerShell at all. I tested this to verify. So if my code is signed and PowerShell is set to AllSigned the script does not run in via Task Scheduler, but does not manually.

    I was attempting to be a good steward of security and keep it AllSigned, but that does not look like it works. So I have set my servers to RemoteSigned. If anyone else has tested this let me know, as I would be interested to hear what others have found.

    Thanks for all the help.

    Nua-O
    • Marked as answer by Nua-O Wednesday, July 1, 2009 12:37 PM
    Wednesday, July 1, 2009 12:37 PM
  • hi, sorry to reply into an ancient thread, but it fits perfectly.

    I have the exact same problem and have been annoyed by it in recent months. (I thought this worked at first, but it hasn't been working for some time. because I couldn't find a solution i just run the job manually once in a while. finally got around to looking for a solution again and found this.)

    alas, the posted solution does NOT work for me. the first thing i ever did when using powershell was

     Set-ExecutionPolicy RemoteSigned

    and that setting is still in force. but I have the exact same symptoms Nua-O listed. history says:

     - task triggered by scheduler

     - task engine received message to start task

     - action started

     - task started

     - created task process

     - action completed

     - task completed

    no errors, but no real action. it all takes about 1 second, but it is much ado about nothing, apparently.

     

    i too am a local admin. i have the script set to run as me whether I am logged on or not.

    might there be better error logging somewhere else?? i don't find anything in the event manager either.

    cheers,

     

    Tim Hanson

     

    Thursday, May 27, 2010 12:10 PM
  • I'm having the same issue.  I created a powershell script and call it from a batch file with output to a log file:

    (powershell.exe -noninteractive -nologo "E:\scripts\sync_live@edu\StartSync.ps1 | Out-File E:\scripts\sync_live@edu\synch.log -append -noclobber")

    I can launch the batch file by double-clicking on it and it runs fine, but calling it from task scheduler, it generates the following error:

    You cannot call a method on a null-valued expression

    Really!!!? Really!!!  :)  I had my scripts set to unrestricted and have used a domain admin account just to troubleshoot.  I've also tried using the above switches - as you can see. 

    It runs like a champ from the command line.  Scheduled task generates the error. . . . . .

    Monday, June 7, 2010 3:55 PM
  • scripterwakkie,

    I know where you are going with this,  I am using StartSync.ps1 as well.

    We are in the middle of testing provisioning in Outlook Live right now.  However, I am entering StartSync.ps1 directly into Task Manager; no batch file.  Folders in my path to the script have spaces in the names, installed by default into the directories, this requires the use of a leading ampersand. Also, I use single quote marks rather than double. Keep in mind I am a Powershell noob, still, I've read that double quotes will have PS aware of variables.  If there are symbols in your file path, it may see it as a variable if double quotes are used.

    Try this as a test then add the log file aspect later...

    Set it up directly within Task Manager in the Edit Action window.  Point it to PowerShell.exe and the script path without the batch file using the following...

    Action:  Start a Program

    Program/script:  Powershell.exe

     (You can browse to insert the full path to be safe, but server 2008 Enterprise found Powershell.exe without it )

     

    Add arguments:  & 'E:\scripts\sync_live@edu\StartSync.ps1'

    (If the underscore is a actually a space in the folder name, replace it with a space. The ampersand will allow the path to include spaces as it is passed to Powershell.  Even if there are no spaces, it won't hurt to leave it in. )

     

    Set it so that you can run it manually within Task Scheduler and test it.  If this works for you, then add the log aspect of the code & test. I use ILM and now see successful Delta operations in my test instance.

     I am using "history" in Task Manager and ILM MA Operation to check operations.  With various path settings in previous tests there were a number of failures. One showed successful tasks completions in Task Manager, but the script did not produce any results in ILM Operations. It also did not throw errors when the script did not run correctly.  Task History did help me get the timing right. At one point it would run correctly once, but would not automate hourly Triggers because the instance was running continuously, and I set it to run only one instance to prevent corruption of the ILM metaverse.  Once I set it to end task after 30 minutes, along with the ampersand and single quotes, it runs properly for me.

    -Will

     

    Sunday, August 15, 2010 9:04 PM
  • I had this same issue and fixed it by running the 32bit version of powershell in Task Manager. Here's my Task Manger Edit Action Program/script: "C:\Windows\syswow64\Windowspowershell\v1.0\powershell.exe" arguments: -WindowStyle Hidden & 'C:\Scripts\ParseMe.ps1' Good Luck ! Dorkygrin
    Tuesday, February 8, 2011 9:37 PM
  • dorkygrin.  You are the man - worked like a charm.  I just had to make sure the 32bit powershell was set to RemoteSigned and bobs your uncle.

    Friday, April 1, 2011 1:45 PM
  • I wanted to add something to this thread, because the information provided here is helpful.

    Make sure that, if you run Powershell with a profile that loads snap-ins, that the same profile is set up for the account under which the script is run in Task Scheduler.  I copied the Users\< My User Name>\Documents\WindowsPowerShell folder to Users\<Script User Name>\Documents\WindowsPowerShell to resolve the issue (where My User Name is the user ID for my own account, and where Script User Name is the user ID for the account the Task Scheduler uses to run the script.

    Cheers!


    Chris Mang>> JDA Software Group, Inc.
    Tuesday, September 6, 2011 2:59 PM
  • This is how I do it:

    In the Program/script, put

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

    In the Add arguments (optional) -noninteractive -nologo d:\backup\createVM.ps1

    Works great for me.

    Karl

    this worked for me

    Windows Server 2012

    Thursday, December 5, 2013 11:07 AM
  • I was having a very similar issue with using the -File param (optional arg in the scheduled task) with a path that included 'Program Files (x86)' - I have no idea if path with spaces played into it at all. 2008 R2 and PS 2.0.

    At any rate, I was seeing the same thing - powershell starts then closes instantly. Script ran fine in an administrative interactive powershell session and in all other testing outside of running as any tested user on the scheduled task. Changing from using the -File parameter to specifying & 'path-to-script' instead and it works like a champ. I, too, am pointing at a signed ps1 file, using the AllSigned execution policy, and the script is signed by a cert that is in the system's trusted publishers store, issued from a trusted CA in my domain.

    I never could find a security violation that was responsible. Really looks like some kinda bug. Any combination of params with -File never produced a working result.

    • Proposed as answer by arnaud.helin Tuesday, January 6, 2015 3:10 PM
    • Unproposed as answer by arnaud.helin Tuesday, January 6, 2015 3:10 PM
    Wednesday, March 19, 2014 11:52 PM
  • I write my solution for persons who are still blocked for running script.

    First of all, you have to be sure that your script is runable manually from the server/computer you want to. (run the script by command line for example).

    The PowerShell execution policy have to be changed to RemoteSigned. (or Unrestricted if you don't have any signed script).

    You have to place your script into a directory and make sure that the user who launch the script is able to read/write the script.

    During the schedule task creation, check "run with all privileges", be sure to use the correct user to launch the script, "run whether use is logged on or not".

    Now, the trick is to go on the Action thumbnail. You have to set "Start a program" as action, set "<absolute_path>\powershell.exe (where <absolute_path> is the powershell.exe path, most of time it will be C:\Windows\System32\WindowsPowershell\v1.0\powershell.exe, set Add arguments (optional) :

    & " '<your_script_path>' ", that is all. (e.g. & " 'C:\Program Files\My_script.ps1' ")

    When I did that, all of my signed script ran like a charm.

    Hope this help you.

    • Proposed as answer by Bankwala Thursday, January 22, 2015 10:05 PM
    Tuesday, January 6, 2015 4:01 PM
  • Many thanks. That worked for me, after having tried many hours with other options.
    Thursday, January 22, 2015 10:05 PM
  • That did the trick!

    Thanks m8 !

    Tuesday, November 28, 2017 8:11 AM