Manage multiple databases services using batch script

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
elzooilogico
Posts: 128
Joined: 23 May 2016 15:39
Location: Spain

Re: Load multiple parameters from file

#16 Post by elzooilogico » 07 Feb 2019 08:44

think you are looking for this but, please
  • ask what is your real task
  • show us what have you tried
as, we are here to help, not to do your own work

now, you may also call the script with a param as scriptname "service_name"

Code: Select all

@echo off
setlocal enabledelayedexpansion
set "MyServiceName=Enter_Name_here"
if not "%~1"=="" set "MyServiceName=%~1"

set "query=wmic service where "name like '%%%%!MyServiceName!%%%%'" get name, state"

echo Testing %MyServiceName% & echo(
  
for /F "skip=1 tokens=1,2 delims= " %%G in ('!query!') do (
  if not "%%G"=="" if not "%%H"=="" (
    echo Service %%G is %%H
    if /I "%%H" == "stopped" (
      rem add code here
    ) else if /I "%%H" == "running" (
      rem add code here
    )
  ) 
)  
endlocal
exit /b

output where set "MyServiceName=Win" or calling script.bat "Win"

Code: Select all

Testing win

Service WinDefend is Stopped
Service WinHttpAutoProxySvc is Running
Service Winmgmt is Running
Service WinRM is Running
btw, you may also change thread title now :mrgreen:

suadhm
Posts: 13
Joined: 05 Feb 2019 05:22

Re: Load multiple parameters from file

#17 Post by suadhm » 08 Feb 2019 02:51

Thank you!

Ok, here is the the thing: I have created one bat script for managing multiple test database on my test virtual machine.
On that machine I have Oracle databases (multiple versions), MSSQL Express, PostgreSQL and Oracle TimesTen database. I run the scripts without parameters, just double click on script shortcut.

Screenshot can be find here:

https://flic.kr/p/Sviobd


Here is its source code:

Code: Select all

@echo OFF&& SETLOCAL&& PUSHD "%~dp0"&& SETLOCAL ENABLEDELAYEDEXPANSION&& SETLOCAL ENABLEEXTENSIONS 
mode 110,30
set BAT_HOME=%~dp0
SET PATH=%BAT_HOME%;%PATH%
color A
echo.
echo.
title ==== [ Suad's DB Manager ] ====

:MENU
set "testdb_stat="
set "xe_stat="
set "%ERRORLEVEL%="
sc query "OracleServiceORA11G" | findstr RUNNING > nul
if %ERRORLEVEL% == 1 set testdb_stat=STOPPED
if %ERRORLEVEL% == 0 set testdb_stat=RUNNING

sc query "OracleServiceXE" | findstr RUNNING  > nul
if %ERRORLEVEL% == 1 set xe_stat=STOPPED
if %ERRORLEVEL% == 0 set xe_stat=RUNNING

set "ora_stat="
sc query "OracleServiceORA12CDB" | findstr RUNNING  > nul
if %ERRORLEVEL% == 1 set ora_stat=STOPPED
if %ERRORLEVEL% == 0 set ora_stat=RUNNING

set "eera_stat="
sc query "OracleServiceORA10G" | findstr RUNNING  > nul
if %ERRORLEVEL% == 1 set eera_stat=STOPPED
if %ERRORLEVEL% == 0 set eera_stat=RUNNING

set "sql_stat="
sc query "MSSQL$SQLEXPRESS" | findstr RUNNING  > nul
if %ERRORLEVEL% == 1 set sql_stat=STOPPED
if %ERRORLEVEL% == 0 set sql_stat=RUNNING

set "psql_stat="
sc query "postgresql-x64-10" | findstr RUNNING  > nul
if %ERRORLEVEL% == 1 set psql_stat=STOPPED
if %ERRORLEVEL% == 0 set psql_stat=RUNNING

set "tt_stat="
sc query "timestend1122.exe" | findstr RUNNING  > nul
if %ERRORLEVEL% == 1 set tt_stat=STOPPED
if %ERRORLEVEL% == 0 set tt_stat=RUNNING


if %xe_stat% == STOPPED set par1=Red
if %xe_stat% == RUNNING set par1=Blue

if %eera_stat% == STOPPED set par2=Red
if %eera_stat% == RUNNING set par2=Blue

if %testdb_stat% == STOPPED set par3=Red
if %testdb_stat% == RUNNING set par3=Blue

if %ora_stat% == STOPPED set par4=Red
if %ora_stat% == RUNNING set par4=Blue

if %sql_stat% == STOPPED set par5=Red
if %sql_stat% == RUNNING set par5=Blue

if %psql_stat% == STOPPED set par6=Red
if %psql_stat% == RUNNING set par6=Blue

if %tt_stat% == STOPPED set par7=Red
if %tt_stat% == RUNNING set par7=Blue

cls
color A
echo.                       
echo.
echo. 
echo                         ---------------------------------------------------------
echo                         [                   Suad's DB Manager                   ]
echo                         ---------------------------------------------------------
echo.                        
echo.                        
echo                                             Select Database
echo                                            ------------------
echo.                        
sfk tell [def]"                            1.  XE (11gR2)                  - State:"[%par1%] %xe_stat%
echo.                                                                      
sfk tell [def]"                            2.  ORA10G (10gR2 EE)           - State:"[%par2%] %eera_stat%
echo.                                                                      
sfk tell [def]"                            3.  ORA11G (11gR2 EE)           - State:"[%par3%] %testdb_stat%
echo.                                                                      
sfk tell [def]"                            4.  ORA12CDB (12gR2 EE)         - State:"[%par4%] %ora_stat%
echo.                                      
sfk tell [def]"                            5.  SQLEXPRESS (SQLServer 2014) - State:"[%par5%] %sql_stat%
echo.                                      
sfk tell [def]"                            6.  PostgreSQL (v10.1)          - State:"[%par6%] %psql_stat%
echo.     
sfk tell [def]"                            7.  TimesTen (11.2.2)           - State:"[%par7%] %tt_stat%
echo.                   
echo.                       
echo                                 ----------------------------------------
echo                                 ============PRESS 'Q' TO QUIT===========
echo.                                          
echo                                          Press ENTER to refresh
echo.
SET INPUT=
SET /P INPUT=Please select Database: 

IF /I '%INPUT%'=='1' GOTO Selection1
IF /I '%INPUT%'=='2' GOTO Selection2
IF /I '%INPUT%'=='3' GOTO Selection3
IF /I '%INPUT%'=='4' GOTO Selection4
IF /I '%INPUT%'=='5' GOTO Selection5
IF /I '%INPUT%'=='6' GOTO Selection6
IF /I '%INPUT%'=='7' GOTO Selection7
IF /I '%INPUT%'=='' GOTO MENU
IF /I '%INPUT%'=='Q' GOTO Quit

CLS
color c
echo                                   ============INVALID INPUT============
echo                                   -------------------------------------
echo                                   Please select a number from the Main
echo                                     Menu [1-7] or select 'Q' to quit
echo                                   -------------------------------------
echo                                   ======PRESS ANY KEY TO CONTINUE======

PAUSE > NUL
GOTO MENU


:Selection1
set xe_stat=
sc query "OracleServiceXE" | findstr RUNNING  
if %ERRORLEVEL% == 1 set xe_stat=STOPPED
if %ERRORLEVEL% == 0 set xe_stat=RUNNING
cls
color a
echo.
echo.
echo.
echo                        ---------------------------------------------------------
echo                                          [ Suad's DB Manager ]
echo                        ---------------------------------------------------------
echo.
echo.
echo                                      Selected Database: XE (11gR2)
echo.
sfk tell [def]"                                          [ State:"[%par1%] %xe_stat% [def]]
echo.
echo.
echo.
echo.
echo.
SET INPUT2=
SET /P INPUT2=Type command [STOP / START / TAIL [Alert Log] / Quit]: 
if '%INPUT2%' == 'Quit' GOTO MENU
if NOT '%INPUT2%' == 'STOP' (
if NOT '%INPUT2%' == 'START' (
::if NOT '%INPUT2%' == 'TAIL' (
goto inval1
		)
   )
)

if '%INPUT2%' == 'TAIL' goto alert_xe

net %INPUT2% OracleServiceXE
net %INPUT2% OracleXETNSListener
GOTO MENU

:alert_xe
cls
O:\Tools\tail C:\oraclexe\app\oracle\diag\rdbms\xe\xe\trace\alert_xe.log
GOTO MENU


:inval1
CLS
color c
echo.
echo.
echo.
echo.
echo                                   ============INVALID INPUT============
echo                                   -------------------------------------
echo.                                   
echo                                      Use command: START, STOP or Quit
echo.
echo                                   -------------------------------------
echo                                   ======PRESS ANY KEY TO CONTINUE======
echo.
echo.
PAUSE > NUL
GOTO Selection1



:Selection3
set testdb_stat=
sc query "OracleServiceORA11G" | findstr RUNNING
if %ERRORLEVEL% == 1 set testdb_stat=STOPPED
if %ERRORLEVEL% == 0 set testdb_stat=RUNNING
cls
color a
echo.
echo.
echo. 
echo                        ---------------------------------------------------------
echo                                          [ Suad's DB Manager ]
echo                        ---------------------------------------------------------
echo.
echo. 
echo                                     Selected Database: ORA11G (11gR2 EE)
echo.      
sfk tell [def]"                                          [ State:"[%par3%] %testdb_stat% [def]]
echo.
echo.
echo.
echo.
echo.
SET INPUT3=
SET /P INPUT3=Type command [STOP / START / TAIL [Alert Log] / Quit]]: 
if '%INPUT3%' == 'Quit' GOTO MENU
if NOT '%INPUT3%' == 'STOP' (
if NOT '%INPUT3%' == 'START' (
if NOT '%INPUT3%' == 'TAIL' (
goto inval3
		)
   )
)

if '%INPUT3%' == 'TAIL' goto alert_10g

net %INPUT3% OracleServiceORA11G
net %INPUT3% OracleOraDb11g_home1TNSListenerLISTENER_11G
GOTO MENU

:alert_10g
cls
O:\Tools\tail O:\Oracle\product\10.2.0\admin\ORA10G\bdump\alert_ora10g.log
GOTO MENU

:inval3
CLS
color c
echo.
echo.
echo.
echo.
echo                                   ============INVALID INPUT============
echo                                   -------------------------------------
echo.                                   
echo                                      Use command: START, STOP or Quit
echo.
echo                                   -------------------------------------
echo                                   ======PRESS ANY KEY TO CONTINUE======
echo.
echo.
PAUSE > NUL
GOTO Selection3


:Selection4
set ora_stat=
sc query "OracleServiceORA12CDB" | findstr RUNNING  
if %ERRORLEVEL% == 1 set ora_stat=STOPPED
if %ERRORLEVEL% == 0 set ora_stat=RUNNING
cls
color a
echo.
echo.
echo.
echo                        ---------------------------------------------------------
echo                                          [ Suad's DB Manager ]
echo                        ---------------------------------------------------------
echo.
echo.
echo                                     Selected Database: ORA12CDB (12gR2)
echo.
sfk tell [def]"                                          [ State:"[%par4%] %ora_stat% [def]]
echo.
echo.
echo.
echo.
echo.
SET INPUT3=
SET /P INPUT3=Type command [STOP / START / TAIL [Alert Log] / Quit]: 
if '%INPUT3%' == 'Quit' GOTO MENU
if NOT '%INPUT3%' == 'STOP' (
if NOT '%INPUT3%' == 'START' (
if NOT '%INPUT3%' == 'TAIL' (
goto inval4
		)
   )
)

if '%INPUT3%' == 'TAIL' goto alert_11g

net %INPUT3% OracleServiceORA12CDB
net %INPUT3% OracleOraDB12Home1TNSListenerLISTENER_12C
net %INPUT3% OracleVssWriterORA12CDB
GOTO MENU

:alert_11g
cls
O:\Tools\tail O:\Oracle\diag\rdbms\ora11g\ora11g\trace\alert_ora11g.log
GOTO MENU


:inval4
CLS
color c
echo.
echo.
echo.
echo.
echo                                   ============INVALID INPUT============
echo                                   -------------------------------------
echo.                                   
echo                                      Use command: START, STOP or Quit
echo.
echo                                   -------------------------------------
echo                                   ======PRESS ANY KEY TO CONTINUE======
echo.
echo.
PAUSE > NUL
GOTO Selection4



:Selection2
set eera_stat=
sc query "OracleServiceORA10G" | findstr RUNNING  
if %ERRORLEVEL% == 1 set eera_stat=STOPPED
if %ERRORLEVEL% == 0 set eera_stat=RUNNING
cls
color a
echo.
echo.
echo.
echo                        ---------------------------------------------------------
echo                                          [ Suad's DB Manager ]
echo                        ---------------------------------------------------------
echo.
echo.
echo                                   Selected Database: ORA10G (10gR2 EE)
echo.
sfk tell [def]"                                          [ State:"[%par2%] %eera_stat% [def]]
echo.
echo.
echo.
echo.
echo.
SET INPUT4=
SET /P INPUT4=Type command [STOP / START / START / TAIL [Alert Log] / Quit]: 
if '%INPUT4%' == 'Quit' GOTO MENU
if NOT '%INPUT4%' == 'STOP' (
if NOT '%INPUT4%' == 'START' (
if NOT '%INPUT4%' == 'TAIL' (
goto inval2
		)
   )
)

if '%INPUT4%' == 'TAIL' goto alert_12c

net %INPUT4% OracleServiceORA10G
net %INPUT4% OracleOraDb10g_home1TNSListenerLISTENER_10G
GOTO MENU

:alert_12c
cls
O:\Tools\tail O:\Oracle\diag\rdbms\ora12cdb\ora12cdb\trace\alert_ora12cdb.log
GOTO MENU

:inval2
CLS
color c
echo.
echo.
echo.
echo.
echo                                   ============INVALID INPUT============
echo                                   -------------------------------------
echo.                                   
echo                                      Use command: START, STOP or Quit
echo.
echo                                   -------------------------------------
echo                                   ======PRESS ANY KEY TO CONTINUE======
echo.
echo.
PAUSE > NUL
GOTO Selection2


:Selection5
set sql_stat=
sc query "MSSQL$SQLEXPRESS" | findstr RUNNING  
if %ERRORLEVEL% == 1 set sql_stat=STOPPED
if %ERRORLEVEL% == 0 set sql_stat=RUNNING
cls
color a
echo.
echo.
echo.
echo                        ---------------------------------------------------------
echo                                          [ Suad's DB Manager ]
echo                        ---------------------------------------------------------
echo.
echo.
echo                              Selected Database: SQLEXPRESS (SQLServer 2014)
echo.
sfk tell [def]"                                          [ State:"[%par5%] %sql_stat% [def]]
echo.
echo.
echo.
echo.
echo.
SET INPUT5=
SET /P INPUT5=Type command [STOP / START / TAIL [ERRORLOG] / Quit]: 
if '%INPUT5%' == 'Quit' GOTO MENU
if NOT '%INPUT5%' == 'STOP' (
if NOT '%INPUT5%' == 'START' (
if NOT '%INPUT5%' == 'TAIL' (
goto inval5
		)
   )
)

if '%INPUT5%' == 'TAIL' goto alert_ms

net %INPUT5% MSSQL$SQLEXPRESS
net %INPUT5% SQLWriter
net %INPUT5% SQLBrowser
GOTO MENU

:alert_ms
cls
"C:\Program Files\Notepad++\notepad++.exe" "C:\Program Files\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQL\Log\ERRORLOG"
GOTO MENU

:inval5
CLS
color c
echo.
echo.
echo.
echo.
echo                                   ============INVALID INPUT============
echo                                   -------------------------------------
echo.                                   
echo                                      Use command: START, STOP or Quit
echo.
echo                                   -------------------------------------
echo                                   ======PRESS ANY KEY TO CONTINUE======
echo.
echo.
PAUSE > NUL
GOTO Selection5


:Selection6
set psql_stat=
sc query "postgresql-x64-10" | findstr RUNNING  
if %ERRORLEVEL% == 1 set psql_stat=STOPPED
if %ERRORLEVEL% == 0 set psql_stat=RUNNING
cls
color a
echo.
echo.
echo.
echo                        ---------------------------------------------------------
echo                                          [ Suad's DB Manager ]
echo                        ---------------------------------------------------------
echo.
echo.
echo                                   Selected Database: PostgreSQL (v10.1)
echo.
sfk tell [def]"                                          [ State:"[%par6%] %psql_stat% [def]]
echo.
echo.
echo.
echo.
echo.
SET INPUT6=
SET /P INPUT6=Type command [STOP / START / LOGS [Open Location] / Quit]: 
if '%INPUT6%' == 'Quit' GOTO MENU
if NOT '%INPUT6%' == 'STOP' (
if NOT '%INPUT6%' == 'START' (
if NOT '%INPUT6%' == 'LOGS' (
goto inval6
		)
   )
)

if '%INPUT6%' == 'LOGS' goto alert_pg

net %INPUT6% postgresql-x64-10
GOTO MENU

:alert_pg
cls
start O:\PostgreSQL\10\data\log
GOTO MENU

:inval6
CLS
color c
echo.
echo.
echo.
echo.
echo                                   ============INVALID INPUT============
echo                                   -------------------------------------
echo.                                   
echo                                      Use command: START, STOP or Quit
echo.
echo                                   -------------------------------------
echo                                   ======PRESS ANY KEY TO CONTINUE======
echo.
echo.
PAUSE > NUL
GOTO Selection6


:Selection7
set tt_stat=
sc query "timestend1122.exe" | findstr RUNNING  
if %ERRORLEVEL% == 1 set tt_stat=STOPPED
if %ERRORLEVEL% == 0 set tt_stat=RUNNING
cls
color a
echo.
echo.
echo.
echo                        ---------------------------------------------------------
echo                                          [ Suad's DB Manager ]
echo                        ---------------------------------------------------------
echo.
echo.
echo                                   Selected Database: TimesTen (11.2.2)
echo.
sfk tell [def]"                                          [ State:"[%par7%] %tt_stat% [def]]
echo.
echo.
echo.
echo.
echo.
SET INPUT7=
SET /P INPUT7=Type command [STOP / START / Quit]: 
if '%INPUT7%' == 'Quit' GOTO MENU
if NOT '%INPUT7%' == 'STOP' (
if NOT '%INPUT7%' == 'START' (
goto inval7
   )
)
net %INPUT7% timestend1122.exe
GOTO MENU

:inval7
CLS
color c
echo.
echo.
echo.
echo.
echo                                   ============INVALID INPUT============
echo                                   -------------------------------------
echo.                                   
echo                                      Use command: START, STOP or Quit
echo.
echo                                   -------------------------------------
echo                                   ======PRESS ANY KEY TO CONTINUE======
echo.
echo.
PAUSE > NUL
GOTO Selection7

:Quit
CLS
title ==== [ Suad's DB Manager - EXIT ] ====
mode 60,15
color 20
echo.
echo.
echo.
echo.
echo            ==============THANK YOU==============
echo            -------------------------------------
echo            ======PRESS ANY KEY TO CONTINUE======
echo.
echo.
echo.
timeout 5
EXIT /b


My plan is to create script that dynamically detects all databases on machine and can manage that databases as my first script.

Regards.

Post Reply