概要概要
最近、同社が開発したサービスプログラムのインストールとアンインストールのスクリプトを作成する必要がありました。インストールサービスとアンインストールサービスは非常にシンプルですが、管理者権限で自動的に実行し、サービスがインストールされているかどうかを判断できるようにしたいです。
ここで、参照用にのみ正しいコードを添付してください。
インストールサービス
@echo off
title 正在获取管理员授权
mode con cols=100 lines=20
color 3f
set name=Shop.Service.Job
set serverpath="%~dp0%name%.exe"
:: 开始获取管理员权限
setlocal
set uac=~uac_permission_tmp_%random%
md "%SystemRoot%\system32\%uac%" 2>nul
if %errorlevel%==0 ( rd "%SystemRoot%\system32\%uac%" >nul 2>nul ) else (
echo set uac = CreateObject^("Shell.Application"^)>"%temp%\%uac%.vbs"
echo uac.ShellExecute "%~s0","","","runas",1 >>"%temp%\%uac%.vbs"
echo WScript.Quit >>"%temp%\%uac%.vbs"
"%temp%\%uac%.vbs" /f
del /f /q "%temp%\%uac%.vbs" & exit )
endlocal
:: 完成获取,下面可以开始写你自己的代码了
title 正在安装%name%
echo "Msg:安装 %name%"
@echo off
sc query %name% |findstr /i "STATE">nul
:: 如果服务已安装,跳转至exist标签;如果未安装,跳转至notexist标签
if not errorlevel 1 (goto exist) else goto notexist
:exist
echo "Msg:检查%name% 已经安装, 开始卸载 %name% 重新安装"
sc query |find /i "%name%" >nul 2>nul
:: 如果服务已启动,跳转至stopservice标签;如果未启动,跳转至stopped标签
if not errorlevel 1 (goto stopservice) else goto deleteservice
:stopservice
echo "Msg:%name% 已启动, 正在停止服务..."
sc stop %name%
goto deleteservice
:deleteservice
echo "Msg:%name% 已安装, 正在卸载 %name%..."
sc delete %name%
goto notexist
:notexist
echo "Msg:检查%name% 未安装, 正在准备安装中..."
sc create %name% binPath=%serverpath% start= auto displayname= "%name%"
sc start %name%
goto checking
:checking
echo "Msg:检查%name% 已经安装, 正在检查启动状态..."
sc query |find /i "%name%" >nul 2>nul
:: 如果服务已启动,跳转至end标签;如果未启动,跳转至stopped标签
if not errorlevel 1 (goto end) else goto stoppped
:stoppped
echo "Msg:%name% 未启动, 正在启动中..."
sc start %name%
goto end
:end
echo "Msg:安装完成,启动正常!"
title 安装%name%完成
pause
アンインストールサービス
@echo off
title 正在获取管理员授权
mode con cols=100 lines=20
color 3f
set name=Shop.Service.Job
:: 开始获取管理员权限
setlocal
set uac=~uac_permission_tmp_%random%
md "%SystemRoot%\system32\%uac%" 2>nul
if %errorlevel%==0 ( rd "%SystemRoot%\system32\%uac%" >nul 2>nul ) else (
echo set uac = CreateObject^("Shell.Application"^)>"%temp%\%uac%.vbs"
echo uac.ShellExecute "%~s0","","","runas",1 >>"%temp%\%uac%.vbs"
echo WScript.Quit >>"%temp%\%uac%.vbs"
"%temp%\%uac%.vbs" /f
del /f /q "%temp%\%uac%.vbs" & exit )
endlocal
:: 完成获取,下面可以开始写你自己的代码了
title 正在卸载%name%
echo "Msg:卸载 %name%"
@echo off
sc query %name% |findstr /i "STATE">nul
:: 如果服务已安装,跳转至exist标签;如果未安装,跳转至end标签
if not errorlevel 1 (goto exist) else goto end
:exist
echo "Msg:检查%name% 已经安装, 开始卸载 %name%"
sc query |find /i "%name%" >nul 2>nul
:: 如果服务已启动,跳转至stopservice标签;如果未启动,跳转至stopped标签
if not errorlevel 1 (goto stopservice) else goto deleteservice
:stopservice
echo "Msg:%name% 已启动, 正在停止服务..."
sc stop %name%
goto deleteservice
:deleteservice
echo "Msg:%name% 已安装, 正在卸载 %name%..."
sc delete %name%
goto end
:end
echo "Msg:卸载 %name% 完成!"
title 卸载 %name% 完成
pause
リブート
@echo off
title 正在获取管理员授权
mode con cols=100 lines=20
color 3f
set name=Shop.Service.Job
set serverpath="%~dp0%name%.exe"
:: 开始获取管理员权限
setlocal
set uac=~uac_permission_tmp_%random%
md "%SystemRoot%\system32\%uac%" 2>nul
if %errorlevel%==0 ( rd "%SystemRoot%\system32\%uac%" >nul 2>nul ) else (
echo set uac = CreateObject^("Shell.Application"^)>"%temp%\%uac%.vbs"
echo uac.ShellExecute "%~s0","","","runas",1 >>"%temp%\%uac%.vbs"
echo WScript.Quit >>"%temp%\%uac%.vbs"
"%temp%\%uac%.vbs" /f
del /f /q "%temp%\%uac%.vbs" & exit )
endlocal
:: 完成获取,下面可以开始写你自己的代码了
title 正在检测是否安装%name%
echo "Msg:正在检测是否安装%name%"
@echo off
sc query %name% |findstr /i "STATE">nul
:: 如果服务已安装,跳转至exist标签;如果未安装,跳转至notexist标签
if not errorlevel 1 (goto exist) else goto notexist
:exist
title 检查%name% 已经安装,准备检查启动状态
echo "Msg:检查%name% 已经安装,准备检查启动状态"
sc query |find /i "%name%" >nul 2>nul
:: 如果服务已启动,跳转至end标签;如果未启动,跳转至stopped标签
if not errorlevel 1 (goto stopservice) else goto stoppped
:stopservice
echo "Msg:检查%name% 启动, 正在停止 %name%"
sc stop %name%
goto stoppped
:notexist
title 检查%name% 未安装, 正在准备安装中
echo "Msg:检查%name% 未安装, 正在准备安装中"
sc create %name% binPath=%serverpath% start= auto displayname= "%name%"
goto checking
:checking
echo "Msg:检查%name% 已经安装, 正在检查启动状态..."
sc query |find /i "%name%" >nul 2>nul
:: 如果服务已启动,跳转至end标签;如果未启动,跳转至stopped标签
if not errorlevel 1 (goto end) else goto stoppped
:stoppped
echo "Msg:%name% 未启动, 正在启动中..."
sc start %name%
goto end
:end
echo "Msg:安装完成,启动正常!"
title 安装%name%完成
pause