Tech

Restart services on remote servers by command

When I had to restart the print spooler service on some servers, I didn't want to log in to servers and do that from the GUI on Services... So I created a .bat file to restart services on remote servers.
You can restart specified services on multiple remote servers.

Prerequisites

  • Windows Server.
  • Ensure that the user executing the command has administrative privileges on each server.
  • The source host must have port 445/tcp open to the target host.

Batch File (.cmd or .bat) Contents

Please replace the hostname (or IP address) and service name as necessary. By adding /user after net use, you will not have to log in to any server, and running the command as an administrator will not be required.
In the first place, net use is not needed if you execute the .bat file with the logged in user with administrator privileges.

@echo off && setlocal

rem Server1
call :RestartService 192.168.0.1 "Spooler"

rem Server2
call :RestartService 192.168.0.2 "Spooler"

rem Server3
call :RestartService 192.168.0.3 "Spooler"

exit /b

rem Restart the service by the argument
rem %1: Remote Server
rem %2: Service Name
:RestartService
    net use \%1ipc$
    sc \%1 stop %2
:DoWhile
    sc \%1 query %2 | findstr STATE | findstr STOPPED
    if %ERRORLEVEL% EQU 0 goto DoWhileExit
    goto DoWhile
:DoWhileExit
    sc \%1 start %2
    net use \%1ipc$ /delete
exit /b

Leave a Reply

Your email address will not be published. Required fields are marked *

x  Powerful Protection for WordPress, from Shield Security
This Site Is Protected By
Shield Security