PowerShell - Restart-Computer doesn't work as scheduled task












2















I have a Windows Server 2012 R2 (PowerShell 4).
There is a ps1 file with a PowerShell script, which contains among other things the command restart-computer.
I also created a cmd file which should be started via Task Scheduler.




  • Everything works as expected when I start the cmd file by double-clicking it.

  • Nearly everything works as expected except the reboot when the cmd is started via Windows Task Scheduler.


So I created a ps1 file which only executes restart-computer and a proper cmd file for testing purposes.
I also tried Restart-Computer -AsJob



powershell.exe -ExecutionPolicy Bypass -File "C:restart-computer123.ps1"


I also created the scheduled task configured for "Windows Server 2012 R2", with administrative credentials, highest privileges. and saved the password.



There is no error message in the event log or scheduler, but the server isn't rebooting.



What am I missing or doing wrong?










share|improve this question

























  • under what useraccount does the task run, what else is in the powershell script?

    – Kage
    Sep 27 '16 at 14:18











  • It runs as local "Administrator". There's nothing else in the "testing" powershell script.

    – StefanK
    Sep 27 '16 at 14:33











  • @StefanK what i'd do to troubleshoot this issue: export the whole $error Variable at the end of your script to a file. maybe also export something to a file before the restart-computer part. if nothing gets created, your script doesn't run. if the error get's exported, you have the error message inside your exported file.

    – SimonS
    Sep 27 '16 at 15:18











  • @SimonS writing the $error variable to a file brings light to the problem! There was an error, that there are other users logged on, and therefor it can't restart. With the "-Force" option it works! With the "-AsJob" option there's no error message, but also no reboot.

    – StefanK
    Sep 28 '16 at 5:12











  • @StefanK ok nice. -AsJob gave no error message/output because it's in a background thread. if you're intrested, here's more information: howtogeek.com/138856/…

    – SimonS
    Sep 28 '16 at 5:45
















2















I have a Windows Server 2012 R2 (PowerShell 4).
There is a ps1 file with a PowerShell script, which contains among other things the command restart-computer.
I also created a cmd file which should be started via Task Scheduler.




  • Everything works as expected when I start the cmd file by double-clicking it.

  • Nearly everything works as expected except the reboot when the cmd is started via Windows Task Scheduler.


So I created a ps1 file which only executes restart-computer and a proper cmd file for testing purposes.
I also tried Restart-Computer -AsJob



powershell.exe -ExecutionPolicy Bypass -File "C:restart-computer123.ps1"


I also created the scheduled task configured for "Windows Server 2012 R2", with administrative credentials, highest privileges. and saved the password.



There is no error message in the event log or scheduler, but the server isn't rebooting.



What am I missing or doing wrong?










share|improve this question

























  • under what useraccount does the task run, what else is in the powershell script?

    – Kage
    Sep 27 '16 at 14:18











  • It runs as local "Administrator". There's nothing else in the "testing" powershell script.

    – StefanK
    Sep 27 '16 at 14:33











  • @StefanK what i'd do to troubleshoot this issue: export the whole $error Variable at the end of your script to a file. maybe also export something to a file before the restart-computer part. if nothing gets created, your script doesn't run. if the error get's exported, you have the error message inside your exported file.

    – SimonS
    Sep 27 '16 at 15:18











  • @SimonS writing the $error variable to a file brings light to the problem! There was an error, that there are other users logged on, and therefor it can't restart. With the "-Force" option it works! With the "-AsJob" option there's no error message, but also no reboot.

    – StefanK
    Sep 28 '16 at 5:12











  • @StefanK ok nice. -AsJob gave no error message/output because it's in a background thread. if you're intrested, here's more information: howtogeek.com/138856/…

    – SimonS
    Sep 28 '16 at 5:45














2












2








2








I have a Windows Server 2012 R2 (PowerShell 4).
There is a ps1 file with a PowerShell script, which contains among other things the command restart-computer.
I also created a cmd file which should be started via Task Scheduler.




  • Everything works as expected when I start the cmd file by double-clicking it.

  • Nearly everything works as expected except the reboot when the cmd is started via Windows Task Scheduler.


So I created a ps1 file which only executes restart-computer and a proper cmd file for testing purposes.
I also tried Restart-Computer -AsJob



powershell.exe -ExecutionPolicy Bypass -File "C:restart-computer123.ps1"


I also created the scheduled task configured for "Windows Server 2012 R2", with administrative credentials, highest privileges. and saved the password.



There is no error message in the event log or scheduler, but the server isn't rebooting.



What am I missing or doing wrong?










share|improve this question
















I have a Windows Server 2012 R2 (PowerShell 4).
There is a ps1 file with a PowerShell script, which contains among other things the command restart-computer.
I also created a cmd file which should be started via Task Scheduler.




  • Everything works as expected when I start the cmd file by double-clicking it.

  • Nearly everything works as expected except the reboot when the cmd is started via Windows Task Scheduler.


So I created a ps1 file which only executes restart-computer and a proper cmd file for testing purposes.
I also tried Restart-Computer -AsJob



powershell.exe -ExecutionPolicy Bypass -File "C:restart-computer123.ps1"


I also created the scheduled task configured for "Windows Server 2012 R2", with administrative credentials, highest privileges. and saved the password.



There is no error message in the event log or scheduler, but the server isn't rebooting.



What am I missing or doing wrong?







windows powershell reboot windows-task-scheduler






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 3 at 8:40









karel

9,27293139




9,27293139










asked Sep 27 '16 at 14:02









StefanKStefanK

234




234













  • under what useraccount does the task run, what else is in the powershell script?

    – Kage
    Sep 27 '16 at 14:18











  • It runs as local "Administrator". There's nothing else in the "testing" powershell script.

    – StefanK
    Sep 27 '16 at 14:33











  • @StefanK what i'd do to troubleshoot this issue: export the whole $error Variable at the end of your script to a file. maybe also export something to a file before the restart-computer part. if nothing gets created, your script doesn't run. if the error get's exported, you have the error message inside your exported file.

    – SimonS
    Sep 27 '16 at 15:18











  • @SimonS writing the $error variable to a file brings light to the problem! There was an error, that there are other users logged on, and therefor it can't restart. With the "-Force" option it works! With the "-AsJob" option there's no error message, but also no reboot.

    – StefanK
    Sep 28 '16 at 5:12











  • @StefanK ok nice. -AsJob gave no error message/output because it's in a background thread. if you're intrested, here's more information: howtogeek.com/138856/…

    – SimonS
    Sep 28 '16 at 5:45



















  • under what useraccount does the task run, what else is in the powershell script?

    – Kage
    Sep 27 '16 at 14:18











  • It runs as local "Administrator". There's nothing else in the "testing" powershell script.

    – StefanK
    Sep 27 '16 at 14:33











  • @StefanK what i'd do to troubleshoot this issue: export the whole $error Variable at the end of your script to a file. maybe also export something to a file before the restart-computer part. if nothing gets created, your script doesn't run. if the error get's exported, you have the error message inside your exported file.

    – SimonS
    Sep 27 '16 at 15:18











  • @SimonS writing the $error variable to a file brings light to the problem! There was an error, that there are other users logged on, and therefor it can't restart. With the "-Force" option it works! With the "-AsJob" option there's no error message, but also no reboot.

    – StefanK
    Sep 28 '16 at 5:12











  • @StefanK ok nice. -AsJob gave no error message/output because it's in a background thread. if you're intrested, here's more information: howtogeek.com/138856/…

    – SimonS
    Sep 28 '16 at 5:45

















under what useraccount does the task run, what else is in the powershell script?

– Kage
Sep 27 '16 at 14:18





under what useraccount does the task run, what else is in the powershell script?

– Kage
Sep 27 '16 at 14:18













It runs as local "Administrator". There's nothing else in the "testing" powershell script.

– StefanK
Sep 27 '16 at 14:33





It runs as local "Administrator". There's nothing else in the "testing" powershell script.

– StefanK
Sep 27 '16 at 14:33













@StefanK what i'd do to troubleshoot this issue: export the whole $error Variable at the end of your script to a file. maybe also export something to a file before the restart-computer part. if nothing gets created, your script doesn't run. if the error get's exported, you have the error message inside your exported file.

– SimonS
Sep 27 '16 at 15:18





@StefanK what i'd do to troubleshoot this issue: export the whole $error Variable at the end of your script to a file. maybe also export something to a file before the restart-computer part. if nothing gets created, your script doesn't run. if the error get's exported, you have the error message inside your exported file.

– SimonS
Sep 27 '16 at 15:18













@SimonS writing the $error variable to a file brings light to the problem! There was an error, that there are other users logged on, and therefor it can't restart. With the "-Force" option it works! With the "-AsJob" option there's no error message, but also no reboot.

– StefanK
Sep 28 '16 at 5:12





@SimonS writing the $error variable to a file brings light to the problem! There was an error, that there are other users logged on, and therefor it can't restart. With the "-Force" option it works! With the "-AsJob" option there's no error message, but also no reboot.

– StefanK
Sep 28 '16 at 5:12













@StefanK ok nice. -AsJob gave no error message/output because it's in a background thread. if you're intrested, here's more information: howtogeek.com/138856/…

– SimonS
Sep 28 '16 at 5:45





@StefanK ok nice. -AsJob gave no error message/output because it's in a background thread. if you're intrested, here's more information: howtogeek.com/138856/…

– SimonS
Sep 28 '16 at 5:45










1 Answer
1






active

oldest

votes


















1














Writing the $error variable to a file brings light to the problem!



$error | Out-File C:error.txt


There was an error, that there are other users logged on, and therefor it can't restart.
With the "-Force" option it works.



Thank you SimonS to get me on the right track!






share|improve this answer























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "3"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1128816%2fpowershell-restart-computer-doesnt-work-as-scheduled-task%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Writing the $error variable to a file brings light to the problem!



    $error | Out-File C:error.txt


    There was an error, that there are other users logged on, and therefor it can't restart.
    With the "-Force" option it works.



    Thank you SimonS to get me on the right track!






    share|improve this answer




























      1














      Writing the $error variable to a file brings light to the problem!



      $error | Out-File C:error.txt


      There was an error, that there are other users logged on, and therefor it can't restart.
      With the "-Force" option it works.



      Thank you SimonS to get me on the right track!






      share|improve this answer


























        1












        1








        1







        Writing the $error variable to a file brings light to the problem!



        $error | Out-File C:error.txt


        There was an error, that there are other users logged on, and therefor it can't restart.
        With the "-Force" option it works.



        Thank you SimonS to get me on the right track!






        share|improve this answer













        Writing the $error variable to a file brings light to the problem!



        $error | Out-File C:error.txt


        There was an error, that there are other users logged on, and therefor it can't restart.
        With the "-Force" option it works.



        Thank you SimonS to get me on the right track!







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Sep 28 '16 at 12:10









        StefanKStefanK

        234




        234






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Super User!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1128816%2fpowershell-restart-computer-doesnt-work-as-scheduled-task%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Mouse cursor on multiple screens with different PPI

            Agildo Ribeiro

            Sometime when accessing a menu: “Ubuntu 16.04 has experienced an internal error”