Script to stop services and gracefully reboot the computer

I have a repetitive task that I need to automate with a batch or VBscript.

 Sometimes I have a list of Windows computers that I need to RDP to then:
 -Stop OpenSsh service
 -Stop MySQL Service and the dependent service (I cannot remember its name)
 -Confirm that the OpenSsh and MySQL and the other independent service are completely stopped
 -Gracefully restart the computer.

 I need a script that reads off notepad a list of computers, lined up properly in a column, and   goes to the top list reads the 1st computer name and executes the tasks I mentioned above.
 However I need the script to generate another text file as log file, which will contain for instance:
 Computer1
 OpenSsh service stopped
 MySQL service stopped
 XYZ service stopped
 Computer1 rebooted

 If any of the services cannot be stopped, the script should not reboot the computer, but will log the message in the same text file that this computer has not been rebooted because service XYZ could not be stopped

 Any help will be very much appreciated

 Thank you
jskfanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David ToddSenior DBACommented:
Hi,

Wont psshutdown do much of that for you in one step?

psshutdown -r \\<servername>

Why the need to stop each service individually? Be aware that in attempting to stop a service, control might return to the script prior to the service completing stopping. So you'll need timing and a test for the service to be shutdown ...

HTH
  David
0
Lionel MMSmall Business IT ConsultantCommented:
you can put this in a script to look thorough your list mentioned. however are you familiar with any remote executions tools sc or psexec?

Details on the usage of sc are available at http://support.microsoft.com/kb/166819
and
psexec  https://technet.microsoft.com/en-us/sysinternals/bb545027
0
jskfanAuthor Commented:
I can do that through command prompt.
shutdown /r /m \\remotecomputer

however This will not stop services gracefully...it is like yanking the power cord
0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

David ToddSenior DBACommented:
Hi,

the services should shutdown gracefully. What evidence do you have that they don't?

Regards
  David
0
oBdACommented:
Try this; adjust the ServiceList to your requirements:
@echo off
setlocal enabledelayedexpansion
set ServerList=C:\Temp\test.txt
REM The log file in csv format:
set LogFile=C:\Temp\test.csv
REM Default wait before the reboot starts; during this time, you can still send a "shutdown /m \\<Computer> /a" to abort the shutdown:
set RebootDelay=30
REM Default timeout for the service stop in seconds, if not specified inididually for the service:
set DefaultTimeout=120
REM Service name list (not Display name) in the order in which they should be stopped, optionally followed by a comma and the timeout in seconds.
set ServiceList="ccmsetup,30" "BITS" "Spooler,300" "SNMPTRAP"

set TempFile=%Temp%\sc.tmp
if not exist "%LogFile%" (
	>"%LogFile%" echo "ComputerName","Date","Time","Action","ServiceName","Status","Result"
)
for /f %%c in ('type "%ServerList%"') do (
	echo Processing %%~c:
	set Result=OK
	for %%l in (%ServiceList%) do (
		set ActionDate=!Date!
		set ActionTime=!Time!
		for /f "tokens=1,2 delims=," %%s in ("%%~l") do (
			if "%%~t"=="" (set Timeout=%DefaultTimeout%) else (set Timeout=%%~t)
			<NUL set /p Dummy=  [!ActionTime!] Stopping '%%s', timeout !Timeout!s ... 
			call :StopService %%~c "%%s" !Timeout!
			>>"%LogFile%" echo "%%~c","!ActionDate!","!ActionTime!","StopService","%%s","!Status!","!Result!"
			echo !Result!.
		)
	)
	set ActionDate=!Date!
	set ActionTime=!Time!
	<NUL set /p Dummy=  [!ActionTime!] Sending reboot signal ... 
	call :Reboot %%~c
	>>"%LogFile%" echo "%%~c","!ActionDate!","!ActionTime!","Reboot","","!Status!","!Result!"
	echo !Result!.
)
goto :eof

:StopService <Computer> <Service> <Timeout>
set Computer=%~1
set Service=%~2
set Timeout=%~3
if /i not "!Result!"=="OK" (
	set Result=SKIPPED
	set Status=A preceding service did not stop properly.
	goto :eof
)
sc.exe \\%Computer% query "%Service%" >"%TempFile%" 2>&1
if %errorlevel% equ 0 (
	goto TestState
)
if %errorlevel% equ 1060 (
	set Result=OK
	set Status=Service not found
	goto :eof
)
set Result=ERROR
set Status=
for /f "delims=" %%a in ('type "%TempFile%"') do (set Status=!Status! %%a)
set Status=%Status:~1%
goto :eof
:TestState
for /f "tokens=3 delims=: " %%a in ('type "%TempFile%" ^| find.exe "STATE"') do set ServiceState=%%a
if "%ServiceState%" equ "STOPPED" (
	set Result=OK
	set Status=Already stopped
	goto :eof
)
if "%ServiceState%" neq "RUNNING" (
	set Result=ERROR
	set Status=Unsupported state '%ServiceState%'.
	goto :eof
)
sc.exe \\%Computer% stop "%Service%" >"%TempFile%" 2>&1
set /a Counter = %Timeout%
:LoopWaitForServiceStop
ping.exe -4 -n 2 localhost >NUL
for /f "tokens=3 delims=: " %%a in ('sc.exe \\%Computer% query "%Service%" ^| find.exe "STATE"') do set ServiceState=%%a
if "%ServiceState%" equ "STOPPED" (
	set Result=OK
	set Status=Stopped at %Time%.
	goto :eof
)
set /a Counter -= 1
if %Counter% gtr 0 goto :LoopWaitForServiceStop
set Result=ERROR
set Status=Timeout after %Timeout% seconds; last service state: %ServiceState%.
goto :eof

:Reboot <Computer>
set Computer=%~1
if /i not "!Result!"=="OK" (
	set Result=SKIPPED
	set Status=Not all services were properly stopped.
	goto :eof
)
shutdown.exe /m \\%Computer% /r /f /t %RebootDelay% /d p:0:0 /c "Shutdown initiated by '%~nx0' running on %Computername%." >"%TempFile%"
if %errorlevel% equ 0 (
	set Result=OK
	set Status=Reboot signal successfully sent.
	goto :eof
)
set Result=ERROR
set Status=
for /f "delims=" %%a in ('type "%TempFile%"') do (set Status=!Status! %%a)
set Status=%Status:~1%
goto :eof

Open in new window

0
Jian An LimSolutions ArchitectCommented:
you can use

psexec net stop <service> \\<machinename>


https://technet.microsoft.com/en-au/sysinternals/bb897553.aspx
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jskfanAuthor Commented:
remote using computer management will do the job.
0
jskfanAuthor Commented:
Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.