首页  编辑  

批处理停止以变动的数字尾巴结尾的用户服务

Tags: /计算机文档/Windows应用技巧/   Date Created:
对于Windows 10而言,有一些服务,以数字尾巴结尾,且数字是每次启动后不断变化的。
这些服务,没法在控制面板,系统服务中配置,也没法修改,如果用批处理停止,因为服务名不断变化,所以也不能写死,可以通过下面的方法来实现停止这些数字结尾的服务:

FOR /F "tokens=2 delims= " %%i in ('sc query^|findstr "cbdhsvc_"') DO (net stop %%i)
FOR /F "tokens=2 delims= " %%i in ('sc query^|findstr "CDPUserSvc_"') DO (net stop %%i)
FOR /F "tokens=2 delims= " %%i in ('sc query^|findstr "BluetoothUserService_"') DO (net stop %%i)
FOR /F "tokens=2 delims= " %%i in ('sc query^|findstr "OneSyncSvc_"') DO (net stop %%i)
FOR /F "tokens=2 delims= " %%i in ('sc query^|findstr "WpnUserService_"') DO (net stop %%i)
FOR /F "tokens=2 delims= " %%i in ('sc query^|findstr "UnistoreSvc_"') DO (net stop %%i)
FOR /F "tokens=2 delims= " %%i in ('sc query^|findstr "UserDataSvc_"') DO (net stop %%i)
FOR /F "tokens=2 delims= " %%i in ('sc query^|findstr "UdkUserSvc_"') DO (net stop %%i)
FOR /F "tokens=2 delims= " %%i in ('sc query^|findstr "PrintWorkflowUserSvc_"') DO (net stop %%i)
FOR /F "tokens=2 delims= " %%i in ('sc query^|findstr "MessagingService_"') DO (net stop %%i)
FOR /F "tokens=2 delims= " %%i in ('sc query^|findstr "LxssManagerUser_"') DO (net stop %%i)
FOR /F "tokens=2 delims= " %%i in ('sc query^|findstr "BcastDVRUserService_"') DO (net stop %%i)

参考资料: https://nicelee.top/blog/2021/10/17/win-stop-service/

找到服务的名称
查询运行的服务的信息
sc query

找到相关信息
sc query|findstr “cbdhsvc_”

从相关信息中获取服务名,这里sc query后面的|号需要转义
FOR /F “tokens=2 delims= “ %i IN (‘sc query^|findstr “cbdhsvc_”’) DO (echo %i)

停止服务(将打印信息echo替换为net stop)
FOR /F “tokens=2 delims= “ %i IN (‘sc query^|findstr “cbdhsvc_”’) DO (net stop %i)

当我们写到.bat脚本里的时候,需要注意将%i替换为%%i
FOR /F “tokens=2 delims= “ %%i IN (‘sc query^|findstr “cbdhsvc_”’) DO (net stop %%i)