通过BAT脚本检测并安装WINDOWS服务程序

概述

笔者近日需要为公司开发的一个服务程序写一个安装和卸载的脚本,安装服务和卸载服务都很简单, 但想要自动以管理员权限运行,并且能判断服务是否已经安装.

现在将正确的代码附上,仅供参考.

安装服务

@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

 

  • 5
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

胡老汉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值