104 lines
3.1 KiB
Batchfile
104 lines
3.1 KiB
Batchfile
@echo off
|
|
REM install.bat - Script for installing/uninstalling ProcessMonitoringService on the target machine.
|
|
REM
|
|
REM --- Run as Administrator ---
|
|
REM This script must be run with administrator privileges to create and manage Windows services.
|
|
REM If the script is not run as administrator, it will attempt to restart itself with elevated privileges.
|
|
|
|
setlocal
|
|
|
|
REM Check if the script is running with administrator privileges
|
|
NET SESSION >NUL 2>&1
|
|
IF %ERRORLEVEL% NEQ 0 (
|
|
echo.
|
|
echo This script must be run with administrator privileges.
|
|
echo Attempting to restart with elevated privileges...
|
|
echo.
|
|
powershell -Command "Start-Process -FilePath '%~dpnx0' -Verb RunAs"
|
|
exit /b
|
|
)
|
|
|
|
echo Script is running with administrator privileges.
|
|
|
|
REM --- Service Settings ---
|
|
SET "SERVICE_NAME=ProcessMonitoringService"
|
|
SET "SERVICE_DISPLAY_NAME=Process Monitoring Service"
|
|
SET "SERVICE_DESCRIPTION=A service for detailed monitoring processes and memory regions."
|
|
|
|
REM The service executable is in the same directory as install.bat
|
|
SET "SERVICE_EXE_PATH=%~dp0%SERVICE_NAME%.exe"
|
|
|
|
REM --- Options ---
|
|
SET "ACTION=%1"
|
|
IF "%ACTION%"=="" SET "ACTION=install"
|
|
|
|
echo.
|
|
echo --- Executing action: %ACTION% ---
|
|
echo.
|
|
|
|
IF /I "%ACTION%"=="install" GOTO :INSTALL
|
|
IF /I "%ACTION%"=="uninstall" GOTO :UNINSTALL
|
|
|
|
echo Unknown action: "%ACTION%".
|
|
echo Usage:
|
|
echo %~nx0 install - Creates and starts the service.
|
|
echo %~nx0 uninstall - Stops, deletes the service. After this, manually delete the folder.
|
|
GOTO :END
|
|
|
|
:INSTALL
|
|
echo --- Step 1: Create Windows Service ---
|
|
echo Creating service "%SERVICE_DISPLAY_NAME%" (%SERVICE_NAME%)...
|
|
IF NOT EXIST "%SERVICE_EXE_PATH%" (
|
|
echo ERROR: Service executable not found: "%SERVICE_EXE_PATH%".
|
|
GOTO :END
|
|
)
|
|
|
|
sc create "%SERVICE_NAME%" binPath="\"%SERVICE_EXE_PATH%\"" DisplayName="%SERVICE_DISPLAY_NAME%" start= auto
|
|
IF %ERRORLEVEL% NEQ 0 (
|
|
echo ERROR: Failed to create service. A service with this name might already exist or you lack permissions.
|
|
GOTO :END
|
|
)
|
|
echo Service created successfully.
|
|
|
|
echo --- Step 2: Set Service Description ---
|
|
sc description "%SERVICE_NAME%" "%SERVICE_DESCRIPTION%"
|
|
IF %ERRORLEVEL% NEQ 0 (
|
|
echo WARNING: Failed to set service description.
|
|
)
|
|
|
|
echo --- Step 3: Start Service ---
|
|
echo Starting service "%SERVICE_NAME%"...
|
|
sc start "%SERVICE_NAME%"
|
|
IF %ERRORLEVEL% NEQ 0 (
|
|
echo ERROR: Failed to start service. Check service logs.
|
|
GOTO :END
|
|
)
|
|
echo Service started successfully.
|
|
GOTO :END
|
|
|
|
:UNINSTALL
|
|
echo --- Stop Service ---
|
|
echo Stopping service "%SERVICE_NAME%"...
|
|
sc stop "%SERVICE_NAME%"
|
|
IF %ERRORLEVEL% NEQ 0 (
|
|
echo WARNING: Failed to stop service. The service might not be running or you lack permissions.
|
|
)
|
|
|
|
echo --- Delete Windows Service ---
|
|
echo Deleting service "%SERVICE_NAME%"...
|
|
sc delete "%SERVICE_NAME%"
|
|
IF %ERRORLEVEL% NEQ 0 (
|
|
echo ERROR: Failed to delete service. The service might not exist or you lack permissions.
|
|
GOTO :END
|
|
)
|
|
echo Service deleted successfully.
|
|
|
|
echo For complete removal, please manually delete the folder from which install.bat was run.
|
|
GOTO :END
|
|
|
|
:END
|
|
echo.
|
|
echo Operation completed.
|
|
endlocal
|
|
pause
|