serverRoom/device-agent/install.bat

78 lines
2.0 KiB
Batchfile
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

@echo off
echo ========================================
echo DeviceAgent Windows 服务安装脚本
echo ========================================
echo.
:: 检查管理员权限
net session >nul 2>&1
if %errorLevel% neq 0 (
echo 错误: 请以管理员身份运行此脚本!
pause
exit /b 1
)
:: 设置变量
set SERVICE_NAME=DeviceReportAgent
set DISPLAY_NAME=设备信息上报服务
set EXE_PATH=%~dp0DeviceAgent.exe
:: 检查文件是否存在
if not exist "%EXE_PATH%" (
echo 错误: 找不到 DeviceAgent.exe
echo 请先编译项目: dotnet publish -c Release -o .
pause
exit /b 1
)
:: 停止并删除旧服务(如果存在)
echo 正在检查旧服务...
sc query %SERVICE_NAME% >nul 2>&1
if %errorLevel% equ 0 (
echo 发现旧服务,正在停止...
sc stop %SERVICE_NAME% >nul 2>&1
timeout /t 3 /nobreak >nul
echo 正在删除旧服务...
sc delete %SERVICE_NAME%
timeout /t 2 /nobreak >nul
)
:: 创建服务
echo 正在创建服务...
sc create %SERVICE_NAME% binPath= "%EXE_PATH%" DisplayName= "%DISPLAY_NAME%" start= auto
if %errorLevel% neq 0 (
echo 错误: 创建服务失败!
pause
exit /b 1
)
:: 设置服务描述
sc description %SERVICE_NAME% "定期上报设备信息UUID、IP地址、硬件配置等到管理服务器"
:: 设置服务恢复选项(失败后自动重启)
sc failure %SERVICE_NAME% reset= 86400 actions= restart/60000/restart/60000/restart/60000
:: 启动服务
echo 正在启动服务...
sc start %SERVICE_NAME%
if %errorLevel% neq 0 (
echo 警告: 服务启动失败,请检查配置文件
) else (
echo 服务启动成功!
)
echo.
echo ========================================
echo 安装完成!
echo ========================================
echo 服务名称: %SERVICE_NAME%
echo 显示名称: %DISPLAY_NAME%
echo.
echo 管理命令:
echo 启动服务: sc start %SERVICE_NAME%
echo 停止服务: sc stop %SERVICE_NAME%
echo 查看状态: sc query %SERVICE_NAME%
echo 卸载服务: sc delete %SERVICE_NAME%
echo.
pause