通过服务名称获取 Windows 服务的 PID

Get the PID of a Windows service by the name of the service

有没有办法通过批处理脚本中的命令通过只知道服务名称来获取 windows 服务的 PID?

试试下面的代码:

FOR /F "tokens=3" %%A IN ('sc queryex %serviceName% ^| findstr PID') DO (SET pid=%%A)
 IF "!pid!" NEQ "0" (
  taskkill /F /PID !pid!
 )
@echo off

for /f "tokens= delims=" %%# in ('
  wmic service where "name='Service'" get ProcessId /format:value
') do (
  for /f "tokens=* delims=" %%$ in ("%%#") do set "%%$"
)

taskkill /pid %ProcessId% /f

直接做就容易多了taskkill /f /fi "SERVICES eq <service_short_name>"