Stop the Service, Restart and Start the service using Powershell












0















I need to Stop the service, and once the service is stopped i need to reboot the system. After the reboot i need to start the service if it is in stopped state using powershell script.



$stage= get-service -name "chrome"

if ( $stage.Status -eq "Running" )
{

Write-Host "The service " $stage.Name "is running"

Stop-Service -Name 'chrome'



}


if ( $stage.Status -eq "Stopped")

{
Restart-Computer -Wait

Start-Service 'chrome'


}









share|improve this question




















  • 1





    Your script doesn't survive a reboot so you either have to run that script from another computer, make your chrome service automatic started or have a scheduled task at startup to start the chrome service. Far easiest is to set the chrome service to automatic in the service manager (what is the default anyway I believe?!)

    – Lieven Keersmaekers
    Nov 23 '18 at 7:25











  • @LievenKeersmaekers Is my condition is right? or i need to change it. because i need to validate it, once the chrome service is stopped then only i have to perform the restart

    – guhan v
    Nov 23 '18 at 7:28











  • Executing Restart-Computer -Wait on your own computer either fails or it ends the script at that point (I don't know, haven't tested it). Either way, in its current form, your Start-Service 'chrome' is never reached. Is there a problem setting the chrome service to automatic and have Windows take care of starting it on reboot?

    – Lieven Keersmaekers
    Nov 23 '18 at 7:43











  • @LievenKeersmaekers chrome is just a example service, am looking for other service. When i execute the script its just stopping the service its not moving to next step to reboot the machine

    – guhan v
    Nov 23 '18 at 7:53






  • 1





    Restart-Computer -Wait doesn't work on the local computer (see my answer here for explanation)

    – James C.
    Nov 23 '18 at 10:43
















0















I need to Stop the service, and once the service is stopped i need to reboot the system. After the reboot i need to start the service if it is in stopped state using powershell script.



$stage= get-service -name "chrome"

if ( $stage.Status -eq "Running" )
{

Write-Host "The service " $stage.Name "is running"

Stop-Service -Name 'chrome'



}


if ( $stage.Status -eq "Stopped")

{
Restart-Computer -Wait

Start-Service 'chrome'


}









share|improve this question




















  • 1





    Your script doesn't survive a reboot so you either have to run that script from another computer, make your chrome service automatic started or have a scheduled task at startup to start the chrome service. Far easiest is to set the chrome service to automatic in the service manager (what is the default anyway I believe?!)

    – Lieven Keersmaekers
    Nov 23 '18 at 7:25











  • @LievenKeersmaekers Is my condition is right? or i need to change it. because i need to validate it, once the chrome service is stopped then only i have to perform the restart

    – guhan v
    Nov 23 '18 at 7:28











  • Executing Restart-Computer -Wait on your own computer either fails or it ends the script at that point (I don't know, haven't tested it). Either way, in its current form, your Start-Service 'chrome' is never reached. Is there a problem setting the chrome service to automatic and have Windows take care of starting it on reboot?

    – Lieven Keersmaekers
    Nov 23 '18 at 7:43











  • @LievenKeersmaekers chrome is just a example service, am looking for other service. When i execute the script its just stopping the service its not moving to next step to reboot the machine

    – guhan v
    Nov 23 '18 at 7:53






  • 1





    Restart-Computer -Wait doesn't work on the local computer (see my answer here for explanation)

    – James C.
    Nov 23 '18 at 10:43














0












0








0








I need to Stop the service, and once the service is stopped i need to reboot the system. After the reboot i need to start the service if it is in stopped state using powershell script.



$stage= get-service -name "chrome"

if ( $stage.Status -eq "Running" )
{

Write-Host "The service " $stage.Name "is running"

Stop-Service -Name 'chrome'



}


if ( $stage.Status -eq "Stopped")

{
Restart-Computer -Wait

Start-Service 'chrome'


}









share|improve this question
















I need to Stop the service, and once the service is stopped i need to reboot the system. After the reboot i need to start the service if it is in stopped state using powershell script.



$stage= get-service -name "chrome"

if ( $stage.Status -eq "Running" )
{

Write-Host "The service " $stage.Name "is running"

Stop-Service -Name 'chrome'



}


if ( $stage.Status -eq "Stopped")

{
Restart-Computer -Wait

Start-Service 'chrome'


}






powershell






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 7:23









Lieven Keersmaekers

47k1188124




47k1188124










asked Nov 23 '18 at 7:18









guhan vguhan v

1




1








  • 1





    Your script doesn't survive a reboot so you either have to run that script from another computer, make your chrome service automatic started or have a scheduled task at startup to start the chrome service. Far easiest is to set the chrome service to automatic in the service manager (what is the default anyway I believe?!)

    – Lieven Keersmaekers
    Nov 23 '18 at 7:25











  • @LievenKeersmaekers Is my condition is right? or i need to change it. because i need to validate it, once the chrome service is stopped then only i have to perform the restart

    – guhan v
    Nov 23 '18 at 7:28











  • Executing Restart-Computer -Wait on your own computer either fails or it ends the script at that point (I don't know, haven't tested it). Either way, in its current form, your Start-Service 'chrome' is never reached. Is there a problem setting the chrome service to automatic and have Windows take care of starting it on reboot?

    – Lieven Keersmaekers
    Nov 23 '18 at 7:43











  • @LievenKeersmaekers chrome is just a example service, am looking for other service. When i execute the script its just stopping the service its not moving to next step to reboot the machine

    – guhan v
    Nov 23 '18 at 7:53






  • 1





    Restart-Computer -Wait doesn't work on the local computer (see my answer here for explanation)

    – James C.
    Nov 23 '18 at 10:43














  • 1





    Your script doesn't survive a reboot so you either have to run that script from another computer, make your chrome service automatic started or have a scheduled task at startup to start the chrome service. Far easiest is to set the chrome service to automatic in the service manager (what is the default anyway I believe?!)

    – Lieven Keersmaekers
    Nov 23 '18 at 7:25











  • @LievenKeersmaekers Is my condition is right? or i need to change it. because i need to validate it, once the chrome service is stopped then only i have to perform the restart

    – guhan v
    Nov 23 '18 at 7:28











  • Executing Restart-Computer -Wait on your own computer either fails or it ends the script at that point (I don't know, haven't tested it). Either way, in its current form, your Start-Service 'chrome' is never reached. Is there a problem setting the chrome service to automatic and have Windows take care of starting it on reboot?

    – Lieven Keersmaekers
    Nov 23 '18 at 7:43











  • @LievenKeersmaekers chrome is just a example service, am looking for other service. When i execute the script its just stopping the service its not moving to next step to reboot the machine

    – guhan v
    Nov 23 '18 at 7:53






  • 1





    Restart-Computer -Wait doesn't work on the local computer (see my answer here for explanation)

    – James C.
    Nov 23 '18 at 10:43








1




1





Your script doesn't survive a reboot so you either have to run that script from another computer, make your chrome service automatic started or have a scheduled task at startup to start the chrome service. Far easiest is to set the chrome service to automatic in the service manager (what is the default anyway I believe?!)

– Lieven Keersmaekers
Nov 23 '18 at 7:25





Your script doesn't survive a reboot so you either have to run that script from another computer, make your chrome service automatic started or have a scheduled task at startup to start the chrome service. Far easiest is to set the chrome service to automatic in the service manager (what is the default anyway I believe?!)

– Lieven Keersmaekers
Nov 23 '18 at 7:25













@LievenKeersmaekers Is my condition is right? or i need to change it. because i need to validate it, once the chrome service is stopped then only i have to perform the restart

– guhan v
Nov 23 '18 at 7:28





@LievenKeersmaekers Is my condition is right? or i need to change it. because i need to validate it, once the chrome service is stopped then only i have to perform the restart

– guhan v
Nov 23 '18 at 7:28













Executing Restart-Computer -Wait on your own computer either fails or it ends the script at that point (I don't know, haven't tested it). Either way, in its current form, your Start-Service 'chrome' is never reached. Is there a problem setting the chrome service to automatic and have Windows take care of starting it on reboot?

– Lieven Keersmaekers
Nov 23 '18 at 7:43





Executing Restart-Computer -Wait on your own computer either fails or it ends the script at that point (I don't know, haven't tested it). Either way, in its current form, your Start-Service 'chrome' is never reached. Is there a problem setting the chrome service to automatic and have Windows take care of starting it on reboot?

– Lieven Keersmaekers
Nov 23 '18 at 7:43













@LievenKeersmaekers chrome is just a example service, am looking for other service. When i execute the script its just stopping the service its not moving to next step to reboot the machine

– guhan v
Nov 23 '18 at 7:53





@LievenKeersmaekers chrome is just a example service, am looking for other service. When i execute the script its just stopping the service its not moving to next step to reboot the machine

– guhan v
Nov 23 '18 at 7:53




1




1





Restart-Computer -Wait doesn't work on the local computer (see my answer here for explanation)

– James C.
Nov 23 '18 at 10:43





Restart-Computer -Wait doesn't work on the local computer (see my answer here for explanation)

– James C.
Nov 23 '18 at 10:43












1 Answer
1






active

oldest

votes


















0














You may need to run the code from different server to achieve your goal. as suggested above, once server rebooted, you can't resume back with the same script.



Other than that, there is small change needs to be done you script. which is, you need to call again below code between your if conditions. So that you will get a expected output



$stage= get-service -name "chrome"






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    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%2fstackoverflow.com%2fquestions%2f53442197%2fstop-the-service-restart-and-start-the-service-using-powershell%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









    0














    You may need to run the code from different server to achieve your goal. as suggested above, once server rebooted, you can't resume back with the same script.



    Other than that, there is small change needs to be done you script. which is, you need to call again below code between your if conditions. So that you will get a expected output



    $stage= get-service -name "chrome"






    share|improve this answer




























      0














      You may need to run the code from different server to achieve your goal. as suggested above, once server rebooted, you can't resume back with the same script.



      Other than that, there is small change needs to be done you script. which is, you need to call again below code between your if conditions. So that you will get a expected output



      $stage= get-service -name "chrome"






      share|improve this answer


























        0












        0








        0







        You may need to run the code from different server to achieve your goal. as suggested above, once server rebooted, you can't resume back with the same script.



        Other than that, there is small change needs to be done you script. which is, you need to call again below code between your if conditions. So that you will get a expected output



        $stage= get-service -name "chrome"






        share|improve this answer













        You may need to run the code from different server to achieve your goal. as suggested above, once server rebooted, you can't resume back with the same script.



        Other than that, there is small change needs to be done you script. which is, you need to call again below code between your if conditions. So that you will get a expected output



        $stage= get-service -name "chrome"







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 12:12









        Thameem JThameem J

        11




        11






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • 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%2fstackoverflow.com%2fquestions%2f53442197%2fstop-the-service-restart-and-start-the-service-using-powershell%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

            Feedback on college project

            Futebolista

            Albești (Vaslui)