• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

Batch file help please.

ShepsCrook

[H]F Junkie
2FA
Joined
Feb 16, 2003
Messages
9,527
So I'm not much of a programmer, but my company has ignored a problem with allowing our customers to restart our applications services easily. So I decided to take it upon myself to write a batch file to allow our customers to double click and it will restart our application services in the proper order. However, there's one thing I have not been able to find any information on.

Sometimes our implementers and customers will disable some of the services that they do not need in the windows services manager. I would ideally like to have the batch file check to see if a service is disabled to avoid wasting time trying to start it.

I also have another service which can take some time to stop depending on the workload it has at the time of the command to stop. So I have a fail safe system to check to see if it's still on, re-issue a stop, wait 30 seconds, re-check rinse and repeat until the service is found to be stopped.

Code:
@ECHO off
ECHO PatientWorks Auto Service Restart
ECHO PW Versions 3.4 - 4.X
ECHO on

::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Creating Event Log
::::::::::::::::::::::::::::::::::::::::::::::::::::::::

eventcreate /l application /t warning /so PW-RESTART /id 1 /d "A system administrator has manually restarted the PatientWorks services by using the PW-RESTART Batch File."

::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Stopping Services
::::::::::::::::::::::::::::::::::::::::::::::::::::::::

NET STOP "PATIENTWORKS SERVER MONITOR"
TIMEOUT 5
NET STOP "PATIENTWORKS DATA SERVICES" /y
TIMEOUT 10
TASKLIST | FIND /I "PWIMPORTSERVER.EXE" >NUL
IF NOT %errorlevel%==0 GOTO STEP2
:STEP2
NET STOP "PATIENTWORKS DATAFILE LISTENER"
TIMEOUT 5
TASKLIST | FIND /I "PWHL7AppServer.EXE" >NUL
IF NOT %errorlevel%==0 GOTO STEP3
:STEP3
NET STOP "PATIENTWORKS HL7 lISTENER"
TIMEOUT 5
TASKLIST | FIND /I "PWJOBServer.EXE" >NUL
IF NOT %errorlevel%==0 GOTO STEP4
:STEP4
NET STOP "PATIENTWORKS JOB SERVER"
TIMEOUT 5
TASKLIST | FIND /I "PWREDIRECTOR.EXE" >NUL
IF NOT %errorlevel%==0 GOTO STEP5
:STEP5
NET STOP "PATIENTWORKS REDIRECTOR"
TIMEOUT 5

::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Timeout while DataServices may stop
::::::::::::::::::::::::::::::::::::::::::::::::::::::::

TASKLIST | FIND /I "PWDATASERVER.EXE" >NUL
IF NOT %errorlevel%==0 GOTO STARTING
IF NOT %errorlevel%==1 GOTO RETRY

:RETRY
TASKLIST | FIND /I "PWDATASERVER.EXE" >NUL
IF NOT %errorlevel%==1 TIMEOUT 30
TASKLIST | FIND /I "PWDATASERVER.EXE" >NUL
IF NOT %errorlevel%==1 GOTO RETRY2
:RETRY2
NET STOP "PATIENTWORKS DATA SERVICES" /y
TIMEOUT 5
TASKLIST | FIND /I "PWDATASERVER.EXE" >NUL
IF NOT %errorlevel%==0 GOTO STARTING
IF NOT %errorlevel%==1 GOTO RETRY

::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Starting Services
::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:STARTING
NET START "PATIENTWORKS DATA SERVICES"
TIMEOUT 10
NET START "PATIENTWORKS DATAFILE LISTENER"
TIMEOUT 5
NET START "PATIENTWORKS HL7 lISTENER"
TIMEOUT 5
NET START "PATIENTWORKS JOB SERVER"
TIMEOUT 5
NET START "PATIENTWORKS REDIRECTOR"
TIMEOUT 5
NET START "PATIENTWORKS SERVER MONITOR"
 
The SC command with the QC sub-command will let you detect that a particular service is disabled. On my machine, for example, the SQL AD helper service is disabled right now:

Code:
C:\>sc qc MSSQLServerADHelper100
[SC] QueryServiceConfig SUCCESS

SERVICE_NAME: MSSQLServerADHelper100
        TYPE               : 10  WIN32_OWN_PROCESS
        START_TYPE         : 4   DISABLED
        ERROR_CONTROL      : 1   NORMAL
        BINARY_PATH_NAME   : "C:\Program Files\Microsoft SQL Server\100\Shared\SQLADHLP.EXE"
        LOAD_ORDER_GROUP   :
        TAG                : 0
        DISPLAY_NAME       : SQL Active Directory Helper Service
        DEPENDENCIES       :
        SERVICE_START_NAME : LocalSystem
 
So then I'd just write a command that says,
IF START_TYPE 4
GOTO (wherever I want to continue)
 
I'm not the best at this either and I'm sure there are more efficient ways of accomplishing this task but the following does work.

It just queries the service and pipes the output to a text file, then runs a "findstr" on the text file to see if the service is in a "stopped" state and saves the state in a variable, then checks if the service state is stopped and if it is it starts the service.

Code:
sc query bthserv > temp.txt
for /f "tokens=1,2,3 delims=: " %%i in ('findstr /i /c:"stopped" temp.txt') do set serviceState=%%k
if (%serviceState%) == (STOPPED) sc start bthserv
set serviceState=
del temp.txt
 
Nice work on the script! I'd have marked this as "impossible" with Windows' scripting layer. You might want to dig further into PowerShell, assuming your customers will allow you to install it on the servers.

On the note of the disabled services, I would go down the path of trying to automatically fail the service upon start (so it doesn't wait for "service control response", or whatever Windows calls it): e.g., maybe set the permissions on the services (PWHL7AppServer.exe for example) to be non-executable, or set up Windows Firewall (> 2008) to prevent these services from running. An immediate failure saves your service startup the time, as it responds "Failed" immediately instead of simply timing out.

Just a thought. Hope things are going well over there. :)
 
Back
Top