PowerShell or batch script for  restart service on remote PC

Posted on 2012-08-23
Last Modified: 2012-10-09
Guys I need some help with PowerShell or batch script which I am not very good in.

I want to stop/kill, delete any temp file and then start a service on remote machine.

Machine name is Leo10
Service name is Leoadmin.exe
Some time I restart service but it doesn’t stop so I kill the process then start and delete some temp folders.

Here is the process which I want in script.

On Leo10 server
Stop Leoadmin.exe
If failed then kill process Leoadmin.exe
Delete file on Leo10 server desktop/temp folder if any
Start Leoadmin.exe service

Thank you
Question by:Vijaytyagi
    LVL 13

    Assisted Solution

    net stop leoadmin
    taskkill /F /IM leoadmin.exe
    del C:\User\Destkop\Temp\*.tmp
    net start leoadmin

    Open in new window

    The "Net Stop" and "Net start" may or may not work depending on what the actual name of the service is displayed as in services.msc.

    Test it before putting it into production.

    EDIT:  This is a batch file.
    LVL 77

    Expert Comment

    by:David Johnson, CD, MVP

    stop-Service -InputObject $(Get-Service -Computer LEO10-Name leoadmin
    #give it a few seconds to stop
    Start-Sleep -s 5
    Stop-Process -InputObject $(Get-Service -Computer LEO10 -Name leoadmin.exe
    start-sleep -s 2
    start-service -InputObject $(Get-Service -Computer LEO10 -Name leoadmin

    Author Comment


    Thanks for your advice but both way (PS and batch script) not able to stop service.

    Killing process is working both way.

    Any other suggestion?

    Thank you
    LVL 5

    Accepted Solution

    sc query leoadmin

    Open in new window

    to see if the servce is STOPPABLE at all.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    We recently had an issue where out of nowhere, end users started indicating that their logins to our terminal server were just showing a "blank screen." After checking the usual suspects -- profiles, shell=explorer.exe in the registry, userinit.exe,…
    I had a question today where the user wanted to know how to delete an SSL Certificate, so I thought that I would quickly add this How to! Article for your reference. WHY WOULD YOU WANT TO DELETE A CERTIFICATE? 1. If an incorrect certificate was …
    This tutorial will walk an individual through configuring a drive on a Windows Server 2008 to perform shadow copies in order to quickly recover deleted files and folders. Click on Start and then select Computer to view the available drives on the se…
    This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now