I have the following code that starts a service called uvnc_service on all hostnames I have in a text file called find.txt (see below). I want to add some sort of check to test if the service is already running on it so if it is - do nothing and output a message to the screen saying that its already running or if its not running on one of the hosts in the find.txt file - start the service and then output/append the hostname to this file.
Can someone help me please?
Thanks
fin开发者_如何转开发d.txt...
pc1
pc2
pc3
...
set service = uvnc_service
for /F %%a in (c:\temp\find.txt) do sc \\%%a start %service% && >> out.txt echo %%a
You can use Service control to query the machine and see the status of the service ie is it running
http://ss64.com/nt/sc.html
Try this
set service = uvnc_service
for /F %%a in (c:\find.txt) do call :servicecheck %%a
:servicecheck
sc \\%1 query %service% | FIND "RUNNING"
IF %ERRORLEVEL% == 0 GOTO STARTSERVICE %1
GOTO END
:STARTSERVICE
sc \\%1 start %service% && >> out.txt echo %1
:END
I cant test it on my machine and i haven't done batch in a while so it might not be perfect
精彩评论