我想获得具有显示名称及其状态的服务列表。
这就是我的尝试:
for /f "tokens=2" %s in ('SC query state^= all ^| find "DISPLAY_NAME"') do @(for /f "tokens=4" %t in ('SC query %s ^| find "STATE"') do @echo %s is %t)
但这只返回有限的服务,如磁盘等。
答案 0 :(得分:1)
对于内置的WMI命令行可执行文件WMIC.exe
,这是一项完美的任务。
从cmd.exe提示符:
For /F "Skip=1 Delims=" %A In ('"WMIC Service Get DisplayName, Name, State"') Do @For /F "Delims=" %B In ("%A") Do @Echo(%B
从批处理文件:
@For /F "Skip=1 Delims=" %%A In ('"WMIC Service Get DisplayName, Name, State"'
) Do @For /F "Delims=" %%B In ("%%A") Do @Echo(%%B
@Pause
答案 1 :(得分:0)
尝试直接在批处理文件中获取powershell的帮助,例如这样(保存为.bat并运行它)。在示例中,我将其键入屏幕并为您提供所有服务,仅运行和仅停止但您可以使用该txt文件的内容基本上执行您想要的操作(使用循环搜索它,将部分保存到变量,等)。
@echo off&cls
pushd %~dp0
echo.
echo List ALL services
pause
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "powershell Get-Service | Out-File services.txt"
type services.txt
echo.
echo List services that are RUNNING only
pause
find "Running" services.txt
echo.
echo List services that are STOPPED only
pause
find "Stopped" services.txt
pause
:cleanup
del /f services.txt