certificate.bat 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. @echo off
  2. echo Configuring hosts file
  3. net session >nul 2>&1
  4. if %errorLevel% neq 0 (
  5. echo Please run this script as an administrator
  6. exit /b
  7. )
  8. set "hostsFile=C:\Windows\System32\drivers\etc\hosts"
  9. set "newEntry=192.168.8.235 exam.habook.local"
  10. if not exist "%hostsFile%" (
  11. echo hosts file does not exist:%hostsFile%
  12. exit /b
  13. )
  14. findstr /v /i /c:"exam.habook.local" "%hostsFile%" > "%hostsFile%.tmp"
  15. if %errorLevel% equ 0 (
  16. move /y "%hostsFile%.tmp" "%hostsFile%" >nul 2>&1
  17. echo Removed all entries containing exam.habook.local
  18. ) else (
  19. echo Failed to remove entries containing exam.habook.local
  20. exit /b
  21. )
  22. echo %newEntry% >> "%hostsFile%"
  23. if %errorLevel% equ 0 (
  24. echo Hosts file configured successfully
  25. ) else (
  26. echo Hosts file configuration failed
  27. exit /b
  28. )
  29. :ImportCert
  30. echo Importing certificate
  31. if not exist "%~dp0certificate.cer" (
  32. echo Certificate file does not exist:%~dp0certificate.cer
  33. exit /b
  34. )
  35. set "certSubject="
  36. for /f "tokens=*" %%i in ('certutil -dump "%~dp0certificate.cer" ^| findstr /i "CN="') do (
  37. set "certSubject=%%i"
  38. )
  39. if defined certSubject (
  40. echo Deleting existing certificate with the same name
  41. certutil -delstore "Root" "%certSubject%"
  42. if %errorLevel% equ 0 (
  43. echo Existing certificate deleted successfully
  44. ) else (
  45. echo Failed to delete existing certificate (may not exist)
  46. )
  47. )
  48. echo Importing new certificate
  49. certutil -addstore -f "Root" "%~dp0certificate.cer"
  50. if %errorLevel% equ 0 (
  51. echo Certificate imported successfully
  52. ) else (
  53. echo Certificate import failed
  54. )
  55. echo All operations completed