Stop/start remote services.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Nadeem
Posts: 1
Joined: 23 Mar 2013 11:30

Stop/start remote services.

#1 Post by Nadeem » 23 Mar 2013 12:13

Hi Friends,



I have a requirement to stop/ start and change the start up Auto/Manual of the service on remote servers.


There are 20 servers on which i have to perform the activity almost daily basis.
The services varies from server to server. For eg: 4 DataBase server, 4 Data Mart server, 4 Application server.


I have prepared simple batch file to perform the task.



2 Primary batch files

1A Start Batch file

2B Stop Batch File.

eg of 1A:
@Echo on
Call C:\Final\Secondary\Start\1AA.bat



4 Secondary Start Batch files

1AA

2AA
3AA

4AA

eg of 1AA
@Echo off
for /f %%a in (C:\Final\Module\DBserver.txt) do sc \\%%a start "SQL" >> C:\Final\Logs\logs.txt
for /f %%a in (C:\Final\Module\DBserver.txt) do sc \\%%a config "SQL" start= auto >> C:\Final\Logs\logs.txt
for /f %%a in (C:\Final\Module\DBserver.txt) do sc \\%%a start "FAX" >> C:\Final\Logs\logs.txt
for /f %%a in (C:\Final\Module\DBserver.txt) do sc \\%%a config "FAX" start= auto >> C:\Final\Logs\logs.txt
pause

note: I have created several txt files like DBserver, application etc. to make it simple. I just copy the hostnames under each files.



4 Secondary Stop Batch Files

1BB
2BB
3BB
4BB

eg of 1BB:->
@Echo off
for /f %%a in (C:\Final\Module\DBserver.txt) do sc \\%%a Stop "SQL" >> C:\Final\Logs\logs.txt
for /f %%a in (C:\Final\Module\DBserver.txt) do sc \\%%a config "SQL" start= Demand >> C:\Final\Logs\logs.txt
for /f %%a in (C:\Final\Module\DBserver.txt) do sc \\%%a Stop "FAX" >> C:\Final\Logs\logs.txt
for /f %%a in (C:\Final\Module\DBserver.txt) do sc \\%%a config "FAX" start= Demand >> C:\Final\Logs\logs.txt
pause




where 1A files has instruction to call 1AA, and 1AA checks the DBserver notepad where i have configured the hostnames of the SQL servers. and it stops the services.

My Requirement:
1) I want to have a 60 sec gap between SQL and FAX services.
2) the output in a log file where it should just capture the hostname and if its service has successfully stopped/started.
3) Please prvide me an additional script If there is any other method to use menu to perform below steps.

eg:
1 DB server
2 Data mart
3 application

selecting option 1 should go to the 1AA batch file and run the script accordingly.


Big Thanks in Advance.

sourbread
Posts: 23
Joined: 26 Apr 2012 09:10

Re: Stop/start remote services.

#2 Post by sourbread » 26 Mar 2013 07:05

You might try this and see if it works...

Code: Select all

:sql
for /f %%a in (C:\Final\Module\DBserver.txt) do sc \\%%a start "SQL" >> C:\Final\Logs\logs.txt
if %errorlevel% == 0 (
for /F "skip=1 tokens=*" %%a in ('wmic computersystem get name') do if not defined model set computername=%%b
echo SQL service has successfully started on %%b >> Results.txt
) else if %errorlevel% == 1 (
for /F "skip=1 tokens=*" %%a in ('wmic computersystem get name') do if not defined model set computername=%%b
echo SQL service did NOT start on %%b >> Results.txt
)
for /f %%a in (C:\Final\Module\DBserver.txt) do sc \\%%a config "SQL" start= auto >> C:\Final\Logs\logs.txt

:60seconds
ping localhost -n 1 -w 60000 >nul

:fax
for /f %%a in (C:\Final\Module\DBserver.txt) do sc \\%%a start "FAX" >> C:\Final\Logs\logs.txt
if %errorlevel% == 0 (
echo FAX service has successfully started on %%b >> Results.txt
) else if %errorlevel% == 1 (
echo FAX service did NOT start on %%b >> Results.txt
)
for /f %%a in (C:\Final\Module\DBserver.txt) do sc \\%%a config "FAX" start= auto >> C:\Final\Logs\logs.txt

Post Reply