none
Task Scheduler Won't run my script unless a user is logged in. RRS feed

  • General discussion

  • This is my script

    param (
        [string]$rebootstatus
    )
    $rebootstatus = Get-WURebootStatus -Silent
    If ($rebootstatus -eq 'True') {
        restart-computer
        }

    I am very new to scripting with powershell and don't know a whole lot. I know that I can set the computer to automatically reboot in group policy, but I still want to figure out why this is not working. I have downloaded the Windows Update powershell module and put it in the correct folder so I don't have to import the module each time. I can run the script file just fine when I click on the .ps1 file and run it. It will restart the computer if updates have been installed and it is pending restart. But when running it from task scheduler, it won't work unless a user is logged in. With the task set to "run whether a user is logged in or not" the task will not run. I don't care about different solutions, I am trying to figure out why it is not running. The "why" may be more important down the road than a different solution. Currently I have the task set to run at 4am every day.

    It will launch: powershell

    also tried: c:\windows\system32\WindowsPowershell\1.0\[pwershell.exe

    arguments: c:\script\rebootstatus.ps1

    also tried: -file "c:\script\rebootstatus.ps1"

    It is being run with a domain admin account. I have tried running the script on my domain controller as well as multiple virtual machines that are just domain members and none of them will run the script with the setting of "run whether a user is logged in or not."

    What do I need to do to get this working?

    • Changed type Bill_Stewart Friday, July 7, 2017 6:11 PM
    • Moved by Bill_Stewart Friday, July 7, 2017 6:11 PM This is not "teach me how to use task scheduler" forum
    Wednesday, May 10, 2017 8:15 PM

All replies

  • Sounds like a question for the task-scheduler people and not a scripting issue.  You have already proven that the script works correctly.

    You need to look in the scheduler logs to see what errors you may be getting.

    None of this has anything to do with PowerShell.  It has to do with how you are scheduling this.

    Also be sure you are running with the highest privileges.

    Note that the module you are calling may not be loading if it is not installed globally. Try loading it with an explicit path.


    \_(ツ)_/


    • Edited by jrv Wednesday, May 10, 2017 8:28 PM
    Wednesday, May 10, 2017 8:26 PM
  • This is all you need for a script:

    Get-WURebootStatus -Silent -AutoReboot

    This will reboot if the status is requiring a reboot.  Just run PowerShell in the scheduler like this:

    Command: PowerShell
    Arguments: -ExecutionPolicy Bypass -command "Get-WURebootStatus -Silent -AutoReboot"

    Account: local admin privileges
    Run with highest: Yes


    \_(ツ)_/

    Wednesday, May 10, 2017 8:37 PM
  • I could not find a task scheduler forum, but I have posted it under the Windows Server 2012 General forum as well. Also I know there are many things I can do differently to get server restarts working, but I don't care about that. I am trying to find out what is happening and why this specific thing isn't working. It is bound to happen in the future with other scripts. I want to know why this script isn't working in task scheduler.

    I am not getting any errors in the logs. Just events saying event started and completed, but the server is still pending reboot.

    I have also been running each task as highest privilege.

    Thursday, May 11, 2017 12:21 AM
  • Well the rest of us don't have this issue so you are either defining things incorrectly or you are missing key information.

    Did you even try the script I posted?


    \_(ツ)_/

    Thursday, May 11, 2017 12:24 AM
  • It says the task is running, but it is just not doing anything. When I have it set to" run only when a user is logged in" and I run the task, it will pop up a powershell window and then run the task and restart the server. When I have it set to "run whether a user is logged in or not" and I run the task, nothing will come up. I read some things about some people needing their script to run interactively, but I don't need/want this. Do I need to do something so it will run without interaction?
    Thursday, May 11, 2017 12:28 AM
  • You still didn't answer my question.  Did you run my command?


    \_(ツ)_/

    Thursday, May 11, 2017 12:30 AM
  • After reviewing the code this is what is required:

    Get-WURebootStatus  -AutoReboot

    The "-silent" causes it to skip the reboot.


    \_(ツ)_/

    Thursday, May 11, 2017 12:35 AM
  • This is all you need for a script:

    Get-WURebootStatus -Silent -AutoReboot

    This will reboot if the status is requiring a reboot.  Just run PowerShell in the scheduler like this:

    Command: PowerShell
    Arguments: -ExecutionPolicy Bypass -command "Get-WURebootStatus -Silent -AutoReboot"

    Account: local admin privileges
    Run with highest: Yes


    \_(ツ)_/

    I have changed task scheduler to run your command. Your command will also not work if the option is "run whether users are logged in or not."
    Thursday, May 11, 2017 12:39 AM
  • This works for me:

    Get-WURebootStatus  -AutoReboot


    \_(ツ)_/

    Thursday, May 11, 2017 12:44 AM
  • After reviewing the code this is what is required:

    Get-WURebootStatus  -AutoReboot

    The "-silent" causes it to skip the reboot.


    \_(ツ)_/

    That worked, but again does not solve my problem. I don't care how it works. I could have done 5 other things to get it working. I want to know why it isn't working the way I did it. Why in the future, when I try to do things like this, they won't work. I want to know why the script isn't running through task scheduler.

    Thursday, May 11, 2017 12:46 AM
  • I guess that is something you will have to analyze.  The first requirement fo any tech is to learn how to troubleshoot an issue, Since we can't see your system we cannot do this.  That is the job you have volunteered to have.  It is a critical and fundamental requirement for all who want to be technicians.

    You have to learn how to use concepts like trace statements.  You also need to learn how to read the scheduler logs. Start with these things and learn them.  They will help you t debug many things quickly and without help/

    As a direct criticism the following is just lame.

    param (
         [string]$rebootstatus
     )
     $rebootstatus = Get-WURebootStatus -Silent
     If ($rebootstatus -eq 'True') {
         restart-computer
         }

    This is better stated as this:

     if(Get-WURebootStatus -Silent)}
         restart-computer -force
     }

    I will leave it to you to understand why.

    All of this can be learned by taking a course in PS or studying one of the many good books on PS.  YOu can also use "HELP" to understand how ach CmdLet is designed to work.

    Don't be helpless.  Learn how to empower yourself with PowerShell.  You cannot guess.  You must study how it works at all levels.


    \_(ツ)_/

    Thursday, May 11, 2017 12:58 AM
  • I guess that is something you will have to analyze.  The first requirement fo any tech is to learn how to troubleshoot an issue, Since we can't see your system we cannot do this.  That is the job you have volunteered to have.  It is a critical and fundamental requirement for all who want to be technicians.

    You have to learn how to use concepts like trace statements.  You also need to learn how to read the scheduler logs. Start with these things and learn them.  They will help you t debug many things quickly and without help/

    As a direct criticism the following is just lame.

    param (
         [string]$rebootstatus
     )
     $rebootstatus = Get-WURebootStatus -Silent
     If ($rebootstatus -eq 'True') {
         restart-computer
         }

    This is better stated as this:

     if(Get-WURebootStatus -Silent)}
         restart-computer -force
     }

    I will leave it to you to understand why.

    All of this can be learned by taking a course in PS or studying one of the many good books on PS.  YOu can also use "HELP" to understand how ach CmdLet is designed to work.

    Don't be helpless.  Learn how to empower yourself with PowerShell.  You cannot guess.  You must study how it works at all levels.


    \_(ツ)_/

    Look, I came here looking for help and to learn, not to be told I am doing it wrong and that I need to troubleshoot my own problems. Like every good technician out there, I know when to escalate. I have spent the early hours of my day troubleshooting this issue to figure out why it wasn't working (this is an experiment on my home server, not really a big deal. Just using it to learn). I have tried many other solutions and none have worked. I know I can do many other things to fix the problem, but I don't care about getting it working. I care about why it is not. There is obviously something I haven't configured correctly in task scheduler, so I was checking to see if anyone else ran into this issue. I am by no means an expert on powershell. I can barely use it. I threw that script together within an hour. It was really my first powershell script. I know I have a lot to learn, but I figured this might be a quicker solution. 
    Thursday, May 11, 2017 1:09 AM
  • But you still haven't done any troubleshooting and continue to insist that you script should work.  To be a tech you need to stop aruing and try to understand the lesson. 

    It is not working because you are not doing it correctly.  As you have seen my version works.  Your version fails because you are missing something in your deployment.  We cannot see your system so it is up to you to discover what you are dong that causes this outcome.

    For issues with Task Scheduler  you need to post in another forum.  This s not a TS forum.

    Sorry but we cannot do you r troubleshooting for you.  This is the lonely world of the tech.  Learn to work in this arena if you want to become a tech.

    The most important thing about technology and science is that there are questions. Learn to love question as they are the best path to learning new things.  Stop expecting that someone will give you an answer to everything. 

    You are doing something wrong. We cannot see your system so it is u to you to track this down. That is how you become a real tech.


    \_(ツ)_/

    Thursday, May 11, 2017 1:59 AM
  • But you still haven't done any troubleshooting and continue to insist that you script should work.  To be a tech you need to stop aruing and try to understand the lesson. 

    It is not working because you are not doing it correctly.  As you have seen my version works.  Your version fails because you are missing something in your deployment.  We cannot see your system so it is up to you to discover what you are dong that causes this outcome.

    For issues with Task Scheduler  you need to post in another forum.  This s not a TS forum.

    Sorry but we cannot do you r troubleshooting for you.  This is the lonely world of the tech.  Learn to work in this arena if you want to become a tech.

    The most important thing about technology and science is that there are questions. Learn to love question as they are the best path to learning new things.  Stop expecting that someone will give you an answer to everything. 

    You are doing something wrong. We cannot see your system so it is u to you to track this down. That is how you become a real tech.


    \_(ツ)_/

    I have been troubleshooting. I would not waste my time going to the internet if I hadn't spent a couple of hours trying to figure this out. I have posted multiple things I have tried and they didn't work. I am not expecting anyone to flat out give me the answer because no one has access to my system. The point of posting on forums is to give other people with a fresh mind a chance to see if they have any thoughts or ideas that may help. While yes I could sit here and spend countless hours trying to figure something out, a good tech would know when to ask for help on something they don't know. I have spent my time troubleshooting and I didn't get anywhere. I am not asking for an answer, I am asking for ideas or thoughts that are different than mine which might provide me with a step in the right direction. I am not asking for the response of "not my problem, go fix it yourself."

    I fully understand that it is not working and I fully understand that I am doing something incorrectly. I just don't know what and maybe someone could provide me with an idea that could point me in the correct direction. If I keep doing something wrong, but don't get any constructive criticism or anything as to how I am doing it wrong, will I ever learn to do it right?

    You seem to be under the impression that I tried to get this to work and then after 5 minutes it didn't, I posted to this forum. No that is not what happened. I spent 3-4 hours trying to get this to work. I ran it on SYSTEM accounts, domain admins, local admins, different servers, changed the script, looked at log files, etc... Basically everything I could think of. When I exhausted my ideas, I did what all other techs do. I asked for advice from other techs. I asked for ideas. I asked maybe if someone else has run into a similar issue like this before. All I got were unhelpful responses saying you aren't going to help me. I know you don't flat out have an answer, but if you had a suggestion to try or something would have been helpful.

    I posted this in a windows server 2012 general forum and they seem to think it is a script problem and that I should post here. 

    So please, if you don't have anything helpful or suggestions, then don't say anything. I know you don't flat out have the answer because you aren't some magical windows fairy that can solve all my problems. I am just asking if anything comes to mind that I could try that maybe I have missed. 

    Post from Other Thread:

    1.) I have found that the take will run if it has the user of SYSTEM and no one is logged in.

    2.) It will run with the setting of "run whether a user is logged in or not."

    3.) But when a user logs in and/or the screen locks, it will start the task, but will not run the script and will not provide errors. Logs show everything is working normally.

    1 and 2 will work just fine with each other. 3 Will not work. The task will not run the script when a user is logged in. The user logging in is the domain admin. One user posted that I the user account needs permissions to run the script, which they have because I can run the script as the domain admin and it will work perfectly outside of task scheduler.

    Thursday, May 11, 2017 3:12 AM
  • One user posted that I the user account needs permissions to run the script, which they have because I can run the script as the domain admin and it will work perfectly outside of task scheduler.

    In this case, you are posting your question in the wrong place, as this would be properly a task scheduler question, not a scripting question. (You would have a scripting question if the script itself doesn't work.)

    Please keep in mind that this is a peer-to-peer Q&A forum where we answer specific scripting questions. It happens to be hosted by Microsoft, but it is not an official support channel. We don't have the resources to help you troubleshoot problems you may be having with the scheduler tool.


    -- Bill Stewart [Bill_Stewart]

    Thursday, May 11, 2017 2:23 PM