look_for_running_sids.cmd 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. @Echo off
  2. SetLocal EnableDelayedExpansion
  3. SetLocal EnableExtensions
  4. set v_sql_script=%1 %2 %3
  5. if exist oracle_homes_temp.txt ( del oracle_homes_temp.txt )
  6. if exist oracle_homes_a.txt ( del oracle_homes_a.txt )
  7. rem echo.>oracle_homes_a.txt
  8. rem echo.>oracle_homes_temp.txt
  9. where sqlplus.exe | findstr sqlplus>>oracle_homes_temp.txt
  10. for /f "tokens=*" %%a in ('type oracle_homes_temp.txt') do (
  11. set str=%%a
  12. set str=!str:\BIN\sqlplus.exe=!
  13. >> oracle_homes_a.txt echo !str!
  14. )
  15. for /f "tokens=3" %%a in ('reg query HKLM\SOFTWARE\ORACLE\ /s /v "ORACLE_HOME" ^| findstr "ORACLE_HOME" ') do (
  16. echo %%a>> oracle_homes_a.txt
  17. )
  18. for /f "tokens=3" %%a in ('reg query HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Oracle /s /v "ORACLE_HOME" ^| findstr "ORACLE_HOME"') do (
  19. echo %%a>> oracle_homes_a.txt
  20. )
  21. if exist oracle_homes_temp.txt ( del oracle_homes_temp.txt )
  22. echo CONNECTION_METHOD,ORACLE_HOME_SERVER,ORACLE_SID,OS_USER,TNS_NAME,TNS_HOST,TNS_PORT,TNS_SERVICE_NAME,TNS_SID,DB_USER,SQL,MACHINE_NAME>db_list.csv
  23. set REG_SID=
  24. set V_REG_SID=
  25. set V_OS_USER=
  26. echo "net start | findstr OracleService" >> db_con_coll.log
  27. net start | findstr OracleService >>db_con_coll.log
  28. echo %date%_%time% >> db_con_coll.log
  29. net USER %USERNAME% | findstr "Group" >>db_con_coll.log
  30. for /f "tokens=1" %%n in ( 'net start ^| findstr "OracleService"' ) do (
  31. for /f "tokens=1" %%l in ( 'reg query HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services /s /v "ImagePath" ^| findstr "OracleService"' ) do (
  32. for /f "skip=2 usebackq tokens=2*" %%a in ( `reg query %%l /v ImagePath` ) do (
  33. set "V_REG=%%b"
  34. for %%S in ( %%b ) do call set "REG_SID=%%S"
  35. call :LENGTH !REG_SID! REG_SID_LENGTH
  36. set "V_REG_SID=OracleService!REG_SID!"
  37. if /I "%%n" == "!V_REG_SID!" (
  38. call set V_REG_OH=%%V_REG:~0,-!REG_SID_LENGTH!%%
  39. call set V_REG_OH=%%V_REG_OH:bin\ORACLE.EXE=%%
  40. call :TRIM V_REG_OH
  41. call :TRIM V_REG_OH
  42. rem for /f "tokens=3 usebackq" %%u in (`reg query HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\%%n /v "ObjectName"`) do set V_OS_USER=%%u
  43. set V_OS_USER=%USERNAME%
  44. echo LOCAL,!V_REG_OH!,!REG_SID!,!V_OS_USER!,,,,,,,%v_sql_script%,%ComputerName%>> db_list.csv
  45. )
  46. )
  47. )
  48. )
  49. GOTO :EOF
  50. REM Functions
  51. :UpCase
  52. FOR %%i IN ("a=A" "b=B" "c=C" "d=D" "e=E" "f=F" "g=G" "h=H" "i=I" "j=J" "k=K" "l=L" "m=M" "n=N" "o=O" "p=P" "q=Q" "r=R" "s=S" "t=T" "u=U" "v=V" "w=W" "x=X" "y=Y" "z=Z") DO CALL SET "%1=%%%1:%%~i%%"
  53. GOTO :EOF
  54. :TRIM
  55. SetLocal EnableDelayedExpansion
  56. Call :TRIMSUB %%%1%%
  57. EndLocal & set %1=%tempvar%
  58. GOTO :EOF
  59. :TRIMSUB
  60. set tempvar=%*
  61. GOTO :EOF
  62. :LENGTH
  63. SetLocal
  64. if [%1] EQU [] goto end
  65. :loop
  66. if [%1] EQU [] goto end
  67. set _len=0
  68. set _str=%1
  69. set _subs=%_str%
  70. :getlen
  71. if not defined _subs goto result
  72. :: remove first letter until empty
  73. set _subs=%_subs:~1%
  74. set /a _len+=1
  75. goto getlen
  76. :result
  77. :end
  78. EndLocal & set "%2=%_len%"
  79. GOTO :EOF
  80. EndLocal