我正在编写一个 Windows 批处理脚本来卸载一些软件。但是,我需要在卸载程序完成后等待服务重新启动,然后才能继续下一次卸载。
我可以让脚本等待卸载程序完成使用:-
for /f "usebackq" %%M in ('tasklist /nh /fi "imagename eq %process_1%"') do if not %%M==%ignore_result% goto 1
但是我一生都无法弄清楚如何让脚本等待服务启动,然后再继续执行脚本并运行更多卸载。
我愿意接受任何建议。
我正在编写一个 Windows 批处理脚本来卸载一些软件。但是,我需要在卸载程序完成后等待服务重新启动,然后才能继续下一次卸载。
我可以让脚本等待卸载程序完成使用:-
for /f "usebackq" %%M in ('tasklist /nh /fi "imagename eq %process_1%"') do if not %%M==%ignore_result% goto 1
但是我一生都无法弄清楚如何让脚本等待服务启动,然后再继续执行脚本并运行更多卸载。
我愿意接受任何建议。
如何避免使用中间文件
FOR /F "usebackq tokens=1,4" %%A IN (`sc query AcquisitionService`) DO (
IF %%A==STATE SET serviceStatus=%%B
)
好的,从评论中我已经使用下面的脚本来解决问题。我希望不要使用中间文件,但是当需要时......
@echo off
if exist service.txt del service.txt
set process=setup.exe
set ignore_result=INFO:
:1
for /f "usebackq" %%M in ('tasklist /nh /fi "imagename eq %process%"') do if not %%M==%ignore_result% goto 1
:2
sc query AcquisitionService>service.txt
find "RUNNING"<service.txt>nul
if errorlevel 1 goto 2
:3
echo.
echo Stuff finished.......
感谢您的想法。
海报提供的 anwser 有效,但它使用临时文件。我找到了一种避免使用管道分隔符的方法。
此代码将启动服务,然后等待它启动:
sc start ServiceName
:1
sc query ServiceName>NUL | find "RUNNING">NUL
if errorlevel 1 goto 1
(...)
重定向(> NUL)是可选的,或者至少它们对我的情况没有影响。