批处理是DOS时代比较常用的方法之一,目前来说也是一种高效的方法,复制代码到文本文件中,保存并修改文件扩展名为“*.bat”.
安装windows服务批处理代码如下:
@echo off
set filename=WindowsServiceTest.exe
set servicename=SSSService
pause
echo ============================操作日志==================================== >InstallService.log
if exist "%SystemRoot%\Microsoft.NET\Framework\v2.0.50727" goto netOld
:DispError
echo 您的机器上没有安装 .net Framework 2.0,安装即将终止
echo 您的机器上没有安装 .net Framework 2.0,安装即将终止 >>InstallService.log
goto LastEnd
:netOld
cd %SystemRoot%\Microsoft.NET\Framework\v2.0.50727
echo 您的机器上安装了相应的.net Framework 2.0,可以安装本服务.
echo 您的机器上安装了相应的.net Framework 2.0,可以安装本服务 >>InstallService.log
echo off
echo 清理原有服务项. . .
%SystemRoot%\Microsoft.NET\Framework\v2.0.50727\installutil /U %filename% >> InstallService.log
echo 清理完毕
echo.
echo *********************
echo 安装服务
%SystemRoot%\Microsoft.NET\Framework\v2.0.50727\installutil %filename% >> InstallService.log
echo 启动服务
net start %servicename% >> InstallService.log
echo *********************
echo ======================================================================= >>InstallService.log
type InstallService.log
echo.
echo 操作结束,可以查看日志文件InstallService.log 中具体的操作结果。
:LastEnd
pause
rem exit
卸载windows服务批处理代码如下:
echo off
set filename=WindowsServiceTest.exe
set servicename=SSSService
echo ============================操作日志==================================== >UnInstallService.log
if exist "%SystemRoot%\Microsoft.NET\Framework\v2.0.50727" goto netOld
:DispError
echo 您的机器上没有安装.net FrameWork 2.0,卸载即将终止
echo 您的机器上没有安装.net FrameWork 2.0,卸载即将终止 >>UnInstallService.log
pause
goto LastEnd
:netOld
echo 即将卸载本服务…
echo off
pause
echo *********************
echo 停止服务
net stop %servicename% >>UnInstallService.log
cd %SystemRoot%\Microsoft.NET\Framework\v2.0.50727
%SystemRoot%\Microsoft.NET\Framework\v2.0.50727\installutil /uninstall %filename% >>UnInstallService.log
echo 卸载服务
echo ======================================================================= >>UnInstallService.log
echo *********************
type UnInstallService.log
echo.
echo 操作结束,可以查看日志文件UnInstallService.log中具体的操作结果。
:LastEnd
pause
rem exit