如何在 Windows 中收集每个服务名称及其状态? [英] How to collect each Service name and its Status in Windows?

查看:26
本文介绍了如何在 Windows 中收集每个服务名称及其状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

sc query | findstr SERVICE_NAME

但对于列出的每个 SERVICE_NAME,我还需要 STATUS.

but I also need STATUS for each SERVICE_NAME listed.

推荐答案

这是一个可以完成工作的命令:

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)

工作原理:

第一个 sc 查询 state= all |find "SERVICE_NAME" 正在运行.此命令旨在为您提供服务名称,每行一个.插入符号 ^(我已在此处删除)是必要的,以便转义要影响 sc 命令而不是 for 命令本身.

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 sc command and not the for command itself.

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

Then the initial for /f parses 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:

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

这个输出然后被馈送到下一个 for/f,它运行 sc query servicename,找到带有状态的行,并隔离第 4 个单词"(当前状态).

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).

最后,每个服务的名称与其状态一起打印(此时您可以根据需要选择做一些不同的事情).

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).

重要说明:如果您在批处理文件中运行此程序,则百分号(例如在 %s 处)需要加倍.

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

这篇关于如何在 Windows 中收集每个服务名称及其状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆