如何在Windows中仅获取服务status,替代Linux的systemctl is-active命令
Windows 下等效 systemctl is-active 的实现方案
你要的仅输出状态、适配脚本使用的命令可以通过PowerShell或CMD实现,以下以查询Windows默认防火墙服务(服务名MpsSvc)为例:
方案1:PowerShell(推荐,适配.ps1脚本)
直接查询服务状态,返回Running/Stopped对应原命令的active/inactive:
(Get-Service -Name MpsSvc).Status
如果需要完全和原Linux命令输出一致,直接输出active/inactive字符串,用以下命令:
if ((Get-Service MpsSvc).Status -eq 'Running') { Write-Output 'active' } else { Write-Output 'inactive' }
方案2:CMD命令(适配.bat/.cmd旧脚本)
使用sc查询服务,直接输出active/inactive的命令如下:
sc query MpsSvc | find "RUNNING" >nul && echo active || echo inactive
如果需要查询其他服务,把上述命令中的MpsSvc替换为目标服务的实际服务名即可,服务名可以在services.msc的服务属性页中查看。
内容的提问来源于stack exchange,提问作者kkpareek
相关产品推荐
相关产品推荐

