none
Trying to run PowerShell from scheduled task while logged off RRS feed

  • Question

  • Hi All, I am attempting to run a powershell script from Task Scheduler on Server 2012 R2 SP1. Windows Management Framework has been upgraded to 5.1

    Although I am able to kick off the scheduled task while logged on, I am unable to run the process by schedule when logged off. I have attempted to run the task as a local administrator, SYSTEM and even my own account. I have added the running account to the Local Security Policies "Log on as a service", "Log on locally", Log on as a batch job".

    The Task is set to run whether a user is logged on or not (although that is a greyed out option when leveraging SYSTEM account).

    The task is set to run with highest privileges.

    I have verified the job will run while I am logged on.

    I have tried putting the full program path in for powershell, as well as just "powershell" without quotes.

    I have tried entering the powershell conditions directly in the task (Get-WUInstall -AcceptAll -AutoReboot) as well as placing these conditions in a PS1 file and calling upon it with the following powershell conditions set in the task (-NonInteractive -NoProfile)

    Nothing seems to work. The task simply remains queued with an event id 325.

    Friday, May 19, 2017 4:08 PM

Answers

  • The Task Scheduler can only run an executable. So in order to run a PowerShell script, you must do the following:

    1. Put your code into a .ps1 text file

    2. Run your script file using settings like the following:

    Program/script: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

    Arguments: -NoProfile -ExecutionPolicy Unrestricted -File "C:\My Scripts\My Script.ps1" -Arg1 -Arg2

    If your script runs properly from a PowerShell prompt, then you don't have a scripting question but rather a "how do I use the Task Scheduler" question. Make sure you are using settings like I posted above. Other than that, your query is really outside the scope of a scripting forum.


    -- Bill Stewart [Bill_Stewart]

    Friday, May 19, 2017 4:22 PM
    Moderator

All replies

  • Hi All, I am attempting to run a powershell script from Task Scheduler on Server 2012 R2 SP1. Windows Management Framework has been upgraded to 5.1

    Although I am able to kick off the scheduled task while logged on, I am unable to run the process by schedule when logged off. I have attempted to run the task as a local administrator, SYSTEM and even my own account. I have added the running account to the Local Security Policies "Log on as a service", "Log on locally", Log on as a batch job".

    The Task is set to run whether a user is logged on or not (although that is a greyed out option when leveraging SYSTEM account).

    The task is set to run with highest privileges.

    I have verified the job will run while I am logged on.

    I have tried putting the full program path in for powershell, as well as just "powershell" without quotes.

    I have tried entering the powershell conditions directly in the task (Get-WUInstall -AcceptAll -AutoReboot) as well as placing these conditions in a PS1 file and calling upon it with the following powershell conditions set in the task (-NonInteractive -NoProfile)

    Nothing seems to work. The task simply remains queued with an event id 325.


    • Merged by jrv Friday, May 19, 2017 4:33 PM DUPLICATE
    Friday, May 19, 2017 3:53 PM
  • This is not a scripting issue.  I recommend posting in the OS forum for your version of Windows.  You can also search for articles that will help you to learn how to use the scheduler.


    \_(ツ)_/

    Friday, May 19, 2017 4:07 PM
  • The Task Scheduler can only run an executable. So in order to run a PowerShell script, you must do the following:

    1. Put your code into a .ps1 text file

    2. Run your script file using settings like the following:

    Program/script: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

    Arguments: -NoProfile -ExecutionPolicy Unrestricted -File "C:\My Scripts\My Script.ps1" -Arg1 -Arg2

    If your script runs properly from a PowerShell prompt, then you don't have a scripting question but rather a "how do I use the Task Scheduler" question. Make sure you are using settings like I posted above. Other than that, your query is really outside the scope of a scripting forum.


    -- Bill Stewart [Bill_Stewart]

    Friday, May 19, 2017 4:22 PM
    Moderator
  • DUPLICATE: https://social.technet.microsoft.com/Forums/windowsserver/en-US/2a1a07e3-d6b9-4be4-9720-025127c72510/attempting-to-run-wuinstall-from-scheduled-task-task-remains-queued?forum=winserverpowershell#2a1a07e3-d6b9-4be4-9720-025127c72510

    Please do not ask the same question in a slightly different way.  Pease address the answer given.  This is not  Task Scheduler forum.  You cannot solve your issue with a script change.


    \_(ツ)_/

    Friday, May 19, 2017 4:23 PM
  • Thanks for response Bill, I will give this a try. I will also repost this question in the Server OS General forum. Seems there are some moderators in here who are less helpful than others.... Glad to see there is one helpful one!
    Friday, May 19, 2017 4:27 PM
  • Thanks for the (useless) response. FYI, this was not posted as a "slightly different question". It was verbatim as I was unsure whether this would be of more use in the scripting forum or PowerShell forum the Task Scheduler is a PART of the question, not the whole question.

    get off your high horse and try to be a little more useful if you are going to act as a moderator.

    Friday, May 19, 2017 4:29 PM
  • Thanks for the (useless) response. FYI, this was not posted as a "slightly different question". It was verbatim as I was unsure whether this would be of more use in the scripting forum or PowerShell forum the Task Scheduler is a PART of the question, not the whole question.

    get off your high horse and try to be a little more useful if you are going to act as a moderator.

    It is still not a scripting question and did have a different title.

    Any technician knows that the first thing we do is to study the subsystem, program, service or APS set that we are working with.

    Bill was kind by adding the things you did not post.  Think of whether anyone can be of help if you post no useful information about what you are doing other than it "doesn't work"

    In the  OS forum be sure to post you complete XML of the task so they can see what your configuration settings are.

    In TS you cannot have any input statements or screen output statements as they will not be used.  Input statements and popups will freeze the script until the task times out.


    \_(ツ)_/

    Friday, May 19, 2017 4:39 PM