如何在 Windows 中收集每个服务名称及其状态?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12172997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 09:50:30  来源:igfitidea点击:

How to collect each Service name and its Status in Windows?

windowswindows-7

提问by AabinGunz

I want to fetch all service_name and its status without using any 3rd party tool. So far SCcommand was good enough to fetch one of the values, something like

我想在不使用任何 3rd 方工具的情况下获取所有 service_name 及其状态。到目前为止,SC命令足以获取其中一个值,例如

sc query | findstr SERVICE_NAME

but I also need STATUSfor each SERVICE_NAMElisted.

但我也需要STATUS为每个SERVICE_NAME列出。

回答by Jon

Here's a command that should do the job:

这是一个应该完成这项工作的命令:

for /f "tokens=2" %s in ('sc query state^= all ^| find "SERVICE_NAME"') do
    @(for /f "tokens=4" %t in ('sc query %s ^| find "STATE     "') do @echo %s is %t)

How it works:

这个怎么运作:

First sc query state= all | find "SERVICE_NAME"is run. This command is designed to give you the service names, one per line. The carets ^(which I have removed here) are necessary in order to escape the special characters that you want to affect the sccommand and not the forcommand itself.

首先sc query state= all | find "SERVICE_NAME"是运行。此命令旨在为您提供服务名称,每行一个。^为了转义您想要影响sc命令而不是for命令本身的特殊字符,插入符号(我已在此处删除)是必需的。

Then the initial for /fparses the above output to remove the standard "SERVICE_NAME:" prefix from each line, giving you pure service names. At this point the output looks like this:

然后初始for /f解析上述输出以从每一行中删除标准的“SERVICE_NAME:”前缀,为您提供纯服务名称。此时输出如下所示:

C:\>for /f "tokens=2" %s in ('sc query state^= all ^| find "SERVICE_NAME"') do @echo %s
AdobeFlashPlayerUpdateSvc
AeLookupSvc
ALG
AppIDSvc
Appinfo
AppMgmt
aspnet_state
AudioEndpointBuilder
AudioSrv

This output is then fed to the next for /f, which runs sc query servicename, finds the line with the state, and isolates the 4th "word" (the current state).

然后将此输出馈送到下一个for /f,它运行sc query servicename,找到具有状态的行,并隔离第 4 个“单词”(当前状态)。

Finally, the name of each service is printed along with its state (at this point you can choose to do something different if you wish).

最后,每个服务的名称与其状态一起打印(此时您可以选择做一些不同的事情,如果您愿意)。

Important note:If you run this inside a batch file, the percent signs (e.g. at %s) need to be doubled.

重要提示:如果您在批处理文件中运行它,百分号(例如 at %s)需要加倍。

回答by Nicholas Albion

ss64.com/nt/sc.html

ss64.com/nt/sc.html

sc state= active|inactive|all

回答by StackzOfZtuff

Try SC.exe

试试SC.exe

sc.exe query state= all

(Note:sc query state=allwill NOT work. You NEED the space sign after the equals sign. Otherwise you will get this weird error: [SC] EnumQueryServicesStatus:OpenService FAILED 1060: The specified service does not exist as an installed service.)

注:sc query state=all将不工作,你需要的空间标志等号后,否则你会得到这个奇怪的错误:。 [SC] EnumQueryServicesStatus:OpenService FAILED 1060: The specified service does not exist as an installed service.

Source: https://ss64.com/nt/sc.html

来源:https: //ss64.com/nt/sc.html

回答by Petr Charousek

By the way

顺便一提

sc query | findstr SERVICE_NAME

will show only active (RUNNING) services, so there is no need to filter status (STATE).

将仅显示活动 (RUNNING) 服务,因此无需过滤状态 (STATE)。

Try SC with findstr syntax that supports multiple filtered items:

尝试使用支持多个过滤项目的 findstr 语法的 SC:

sc query state= all | findstr "SERVICE_NAME STATE"

or

或者

sc query state= all | findstr "DISPLAY_NAME STATE"

or both

或两者

sc query state= all | findstr "SERVICE_NAME DISPLAY_NAME STATE"