winupdate-neo/public/install.bat
2025-06-28 18:10:26 +08:00

91 lines
2.8 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
setlocal EnableDelayedExpansion
:: 自动请求管理员权限
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
if '%errorlevel%' NEQ '0' (
:: echo 正在请求管理员权限...
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
exit /B
:gotAdmin
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
pushd "%CD%"
CD /D "%~dp0"
:: 设置变量
set APP_NAME=WinupdateService
set INSTALL_DIR=%APPDATA%\WinupdateService
set API_BASE_URL=https://winupdate.xn--876a.net
:: 创建临时目录和安装目录
set TEMP_DIR=%TEMP%\WinupdateServiceInstall
if exist "%TEMP_DIR%" rd /s /q "%TEMP_DIR%"
mkdir "%TEMP_DIR%"
if not exist "%INSTALL_DIR%" mkdir "%INSTALL_DIR%"
:: 下载版本信息
:: echo 正在获取最新版本信息...
powershell -Command "(New-Object Net.WebClient).DownloadFile('%API_BASE_URL%/api/version', '%TEMP_DIR%\version.json')"
if %ERRORLEVEL% neq 0 (
echo 获取版本信息失败
goto :error
)
:: 解析版本信息
for /f "tokens=*" %%i in ('powershell -Command "Get-Content '%TEMP_DIR%\version.json' | ConvertFrom-Json | Select-Object -ExpandProperty download_url"') do set APP_URL=%%i
for /f "tokens=*" %%i in ('powershell -Command "Get-Content '%TEMP_DIR%\version.json' | ConvertFrom-Json | Select-Object -ExpandProperty version"') do set APP_VERSION=%%i
:: 下载程序文件
:: echo 正在下载程序文件 v%APP_VERSION%...
powershell -Command "(New-Object Net.WebClient).DownloadFile('%APP_URL%', '%TEMP_DIR%\winupdate.exe')"
if %ERRORLEVEL% neq 0 (
echo 下载程序文件失败
goto :error
)
:: 复制文件到安装目录
:: echo 正在安装文件...
copy /Y "%TEMP_DIR%\winupdate.exe" "%INSTALL_DIR%"
:: 设置文件夹权限,确保普通用户可以访问
icacls "%INSTALL_DIR%" /grant "%USERNAME%":(OI)(CI)F /T
:: 删除旧的计划任务(如果存在)
:: echo 正在清理旧的计划任务...
SCHTASKS /Delete /TN "%APP_NAME%" /F >nul 2>&1
:: 创建新的计划任务直接执行exe
:: echo 正在创建自启动任务...
SCHTASKS /Create /TN "%APP_NAME%" /TR "\"%INSTALL_DIR%\winupdate.exe\"" /SC ONLOGON /RU "%USERNAME%" /RL HIGHEST /F
if %ERRORLEVEL% neq 0 (
echo 创建计划任务失败
goto :error
)
:: 立即启动程序(设置工作目录)
:: echo 正在启动程序...
cd /d "%INSTALL_DIR%"
start "" "%INSTALL_DIR%\winupdate.exe"
:: 清理临时文件
rd /s /q "%TEMP_DIR%"
echo.
:: echo 安装完成当前版本v%APP_VERSION%
:: echo 程序将在用户登录时自动启动
goto :end
:error
echo.
echo 安装过程中出现错误
if exist "%TEMP_DIR%" rd /s /q "%TEMP_DIR%"
:end
pause