PowerShell可通过Get-Service的RequiredServices和DependentServices属性分别查询服务启动依赖及被依赖项;需递归调用获取多级依赖,建议限制深度≤4,并可用sc.exe或Get-CimInstance交叉验证。
powershell 提供了直接查询 windows 服务依赖关系的内置命令,最常用的是 get-service 和 get-wmiobject(或更新的 get-ciminstance),但要注意:系统服务的“依赖项”分为两类——服务启动依赖(即该服务启动前必须先运行的服务) 和 被依赖项(即哪些服务依赖于它)。默认命令不直接显示完整依赖树,需组合使用。
查看指定服务的启动依赖(RequiredServices)
Windows 服务对象本身包含 RequiredServices 属性,它列出该服务启动所依赖的其他服务名称(不是 DisplayName)。可用以下命令快速获取:
Get-Service -Name "wuauserv" | Select-Object Name, DisplayName, RequiredServices- 返回结果中
RequiredServices是字符串数组,例如@("rpcss", "cryptsvc"),对应“Remote Procedure Call (RPC)”和“Cryptographic Services” - 注意:此属性仅反映注册表中
DependOnService值,不包含驱动级依赖或延迟启动链
反向查询:找出哪些服务依赖于某服务(DependentServices)
每个服务对象还提供 DependentServices 属性,即“以它为依赖项”的服务列表:
(Get-Service -Name "rpcss").DependentServices | Select-Object Name, DisplayName, Status- 这能帮你判断停用某个基础服务(如
rpcss或eventlog)可能影响哪些上层服务 - 结果中的服务状态可一并查看,便于评估停用风险
获取完整依赖层级(含嵌套依赖)
Windows 不自动展开多级依赖,需递归查询。下面是一个轻量脚本思路:
- 定义函数
Get-ServiceDependencyTree,接收服务名,输出依赖链 - 对每个
RequiredServices再调用自身,逐层向下展开(建议限制深度 ≤4,避免循环或无限递归) - 实际使用时可借助
Get-CimInstance Win32_Service获取更底层字段(如StartMode,State),辅助判断是否已启动
补充:用 sc 命令作快速验证
PowerShell 可调用传统 sc 工具作为交叉验证:
-
sc.exe qc wuauserv显示配置,含DEPENDENCIES行(原始服务名) -
sc.exe enumdepend wuauserv列出直接依赖它的服务(即 DependentServices) - 输出为纯文本,适合脚本解析;PowerShell 中可用
sc.exe qc wuauserv | Select-String "DEPENDENCIES"提取关键行
依赖关系本质由注册表 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\{服务名}\DependOnService 定义,PowerShell 查询的是同一数据源。实际运维中,优先用 Get-Service 查直接依赖,结合 DependentServices 评估影响面,必要时用 sc 或 CIM 实例补全上下文。不复杂但容易忽略层级传导效应。


















