Thursday, November 11, 2010

Net stop command does not work in batch file

We have a batch file with essentially below two lines

netsh adv cons add rule name="blahblah" [big blah blah]
net stop ikeext & NET start ikeext

Usually, this should stop the service  ikeext & then start it again, but it failed to stop the dependent services and thus could not restart. Same sequence works perfect on command prompt.

Add few timeout commands in between and break processing in mulpiple commands. Timeout command is natively available on windows vista onwards.

netsh adv cons add rule name="blahblah" [big blah blah]

net stop ikeext
sc query ikeext   |   findstr   /i   RUNNING   2> NUL   1> NUL
IF  %ERRORLEVEL%   EQU  1  (  NET start "ikeext"  )   else   (  echo "ikeext service already running"  )

No comments: