将以下内容放到新建的.bat文件
脚本示例MySQL服务,更换服务中对应的名称即可启停其他服务,例如Redis服务等
@echo off chcp 65001 >nul setlocal EnableExtensions rem 到服务中找mysql对应名称复制(需要区分大小写) set "SVC=MySQL" rem 无管理员权限时弹出 UAC,提权后重新运行本脚本 net session >nul 2>&1 if errorlevel 1 ( powershell -NoProfile -Command "Start-Process -FilePath '%~f0' -Verb RunAs" exit /b ) echo. echo ========== MySQL 服务开关 ========== echo 服务名: %SVC% echo. set "RUNNING=0" for /f "tokens=*" %%A in ('sc query "%SVC%" 2^>nul ^| findstr /I "RUNNING"') do set "RUNNING=1" if "%RUNNING%"=="1" ( echo 当前状态: 运行中 echo 正在关闭 MySQL ... net stop "%SVC%" if errorlevel 1 (echo [失败] 停止服务失败) else (echo [成功] MySQL 已停止) ) else ( echo 当前状态: 已停止 ^(或未运行^) echo 正在启动 MySQL ... net start "%SVC%" if errorlevel 1 ( echo [失败] 启动服务失败 echo 可尝试: net start %SVC% ) else ( echo [成功] MySQL 已启动 ) ) echo. sc query "%SVC%" | findstr /I "SERVICE_NAME STATE" echo. endlocal exit /b