0

假设我有一个以名称“MyServiceFactory -”开头的服务列表。并非所有这些都会启动,只有少数会启动,并且会因服务使用情况而异。我正在寻求帮助编写一个批处理程序,该程序只停止正在运行的服务并启动这些服务(不是所有服务并且不重新启动)。任何帮助表示赞赏

4

2 回答 2

2

您也可以尝试使用 PowerShell。我有一些用于启动和停止服务的单行代码:

# Start all services with FOO in their name:
powershell -Command start-service *FOO*

# Stop all running FOO services:
powershell -Command stop-service *FOO*

缺点是 PowerShell 命令不会像服务那样​​为您提供有关服务正在发生的情况的状态net start,但您必须喜欢简洁 :)

于 2012-02-16T14:20:37.053 回答
1

这应该有效(或至少给你一个开始):

@echo off 
setlocal
if "%~1"=="" goto usage
set tmpFile=templist.txt
set tmpAnsi=templist_ansi.txt
wmic /locale:MS_409 service where "caption like '%~1' and state='Running'"  get caption /format:csv >%tmpFile%
REM this is required to convert from Unicode(UCS-2) to ANSI
type %tmpFile%>%tmpAnsi%

Echo ---------------Stopping services----------------------
Echo.
for /f "tokens=2 skip=2 delims=," %%i  in (%tmpAnsi%) do (
   wmic /locale:MS_409 service where caption="%%i" call stopservice
)

Echo --------------Starting services-----------------------
Echo.
for /f "tokens=2 skip=2 delims=," %%i  in (%tmpAnsi%) do ( 

   wmic /locale:MS_409 service where caption="%%i" call startservice
)

goto end

:usage
Echo.
Echo Usage is: 
Echo %~n0 pattern_to_check
Echo.
Echo Pattern: 
Echo [ ]  Any one character within the specified range ([a=f]) or set ([abcdef]).
Echo ^^    Any one character not within the range ([^a=f]) or set ([^abcdef].)
Echo %%    Any string of 0 (zero) or more characters
Echo _    (underscore) Any one character. Any literal underscore    
Echo         used in the query string must be escaped by placing it inside []
Echo.
Echo     If pattern contains spaces, it must be enclosed in double quotes 

:end

假设您将批处理文件命名为 batch.bat,您可以将其命名为batch.bat "MyServiceFactory -%".

于 2012-02-16T03:29:59.377 回答