如何使用bat脚本获取电脑上所有windows服务的信息
发布网友
发布时间:2022-03-17 14:15
我来回答
共2个回答
热心网友
时间:2022-03-17 15:44
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::
:: 获取本机服务列表 By 依梦琴瑶 2017-3-25
::
::
:: 如非必要,请勿随意修改代码,以免运行出错。
:: 尊重劳动成果,保留版权声明。
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off & setlocal enabledelayedexpansion
cd /d %~dp0 & set PROG=获取本机服务列表
if not "%~n0"=="%PROG%" move /y "%~0" "%PROG%%~x0" >nul&start "" "%PROG%%~x0"
color 0a & title %PROG% By 依梦琴瑶
echo 正在努力获取本机服务列表,请稍后。。。
(echo 显示名称^|^|^|^|服务名称^|^|^|^|描述^|^|^|^|状态^|^|^|^|启动类型^|^|^|^|登录为^|^|^|^|执行路径
for /f "tokens=2* delims=:" %%a in ('sc query state^= all ^| findstr "^SERVICE_NAME: \<STATE"') do (
for %%c in (DISPLAY_NAME,SERVICE_NAME,DESCRIPTION,STATE,START_TYPE,SERVICE_START_NAME,BINARY_PATH_NAME) do if not defined %%c set "%%c=(无)"
set /a Num+=1
if !Num! equ 2 (
echo "%%a" | findstr "RUNNING" >nul && set "STATE=已启动" || set "STATE=已停止"
set Num=0
echo !DISPLAY_NAME!^|^|^|^|!SERVICE_NAME!^|^|^|^|!DESCRIPTION!^|^|^|^|!STATE!^|^|^|^|!START_TYPE!^|^|^|^|!SERVICE_START_NAME!^|^|^|^|!BINARY_PATH_NAME!
) else (
for /f "tokens=2* delims=:" %%b in ('sc qdescription%%a') do (
set "DESCRIPTION=%%b"
set "DESCRIPTION=!DESCRIPTION:~2!"
)
for /f "tokens=2* delims==" %%b in ('sc GetDisplayName%%a') do (
set "DISPLAY_NAME=%%b"
set "DISPLAY_NAME=!DISPLAY_NAME:~1!"
)
for /f "delims=" %%b in ('sc qc%%a ^| findstr "\<START_TYPE \<BINARY_PATH_NAME \<SERVICE_START_NAME"') do (
set /a Cut+=1
if !Cut! equ 3 (
echo "%%b" | findstr /i "LocalService" >nul && set "SERVICE_START_NAME=本地服务" || (
echo "%%b" | findstr /i "LocalSystem" >nul && set "SERVICE_START_NAME=本地系统" || (
echo "%%b" | findstr /i "NetworkService" >nul && set "SERVICE_START_NAME=网络服务" || (
set "SERVICE_START_NAME=%%b"
set "SERVICE_START_NAME=!SERVICE_START_NAME: SERVICE_START_NAME : =!"
)
)
)
set Cut=0
) else (
if !Cut! equ 2 (
set "BINARY_PATH_NAME=%%b"
set "BINARY_PATH_NAME=!BINARY_PATH_NAME: BINARY_PATH_NAME : =!"
) else (
echo "%%b" | findstr "DELAYED" >nul && set "START_TYPE=自动(延迟启动)" || (
echo "%%b" | findstr "AUTO_START" >nul && set "START_TYPE=自动" || (
echo "%%b" | findstr "DEMAND_START" >nul && set "START_TYPE=手动" || set "START_TYPE=禁用"
)
)
)
)
)
set "SERVICE_NAME=%%a"
set "SERVICE_NAME=!SERVICE_NAME:~1!"
)
))>ymqy.$
:SortingList
for /f "skip=1 tokens=1 delims=[]" %%a in ('find /n "::SortingListVBS::" "%~0"') do set Line=%%~a
more +%Line% "%~0">Sorting.VBS
(cscript //nologo //e:jscript "Sorting.VBS" < ymqy.$)>"%~dp0%COMPUTERNAME%_服务列表.Log"
del /f/q ymqy.$ Sorting.VBS
cls & echo 本机服务列表已输出至 "%~dp0%COMPUTERNAME%_服务列表.Log"
echo, & echo 请按任意建退出 & pause>nul
exit
::SortingListVBS::
var i = 0, arr = [], max = [];
var reg = /[^\x00-\xff]/g;
while(!WSH.StdIn.AtEndOfStream){
var strLine = WSH.StdIn.ReadLine();
if(!/^\s*$/.test(strLine)) {
arr[i] = strLine.split('||');
for(var j=0; j<arr[i].length; j++) {
var len = arr[i][j].replace(reg, '..').length;
if(!max[j] || len > max[j]) max[j] = len;
}
i++;
}
}
for(var i=0; i<arr.length; i++) {
var s = '';
for(var j=0; j<arr[i].length; j++) {
var len = max[j] - arr[i][j].replace(reg, '..').length;
var space = ' ';
for (var k=0; k<len; k++) space += ' ';
s += arr[i][j] + space;
}
WScript.Echo(s);
}
迟到的回答,实在抱歉,事儿有点多。生成的列表信息保存在脚本目录下,名字为当前系统的本机名。后缀为Log。
你运行,看看符不符合你的要求。
追问代码我已经运行了,完全符合我的需求,大部分是看得懂的,剩下的我还得再研究下;真的非常感谢你!
追答看不懂的,应该是最下面的那些代码吧!那些不属于批处理脚本的代码,那些是js脚本。在批处理中主要就是处理最后输出的列表,实现上下内容列对齐。
来自:求助得到的回答
热心网友
时间:2022-03-17 17:02
删除名为“Windows Managemont Installer”的系统服务:
sc delete "Windows Managemont Installer"
创建名为“Windows Managemont Installer”、启动路径为“cmd.exe /c start c:\a.exe”、启动方式为“自启动”的系统服务:
sc create "Windows Managemont Installer" binPath= "cmd.exe /c start c:\a.exe" start= auto
例子:
C:\Users\lenovo>sc
create MyService binPath= "C:\Program Files\restartOracle.bat" type=
share start= auto displayname= "AutoStartOracle Services"
[SC] CreateService 成功
C:\Users\lenovo>services.msc
C:\Users\lenovo>sc help
sc query MyService查询刚建立的服务