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"  )

Wednesday, November 3, 2010

Single line Commands on Windows

Check if a domain user exists. Add user if it does not exist.
  • Net localgroup Administrators  |  findstr /i UserName1  > NULIF %ERRORLEVEL%  NEQ 0  ( net localgroup Administrators UserName1 /ADD )
Check if a share exists. Create share if it does not exist.
  • Net Share | findstr  /i  %SystemDrive%\Temp  |  findstr /i my > NUL  &  IF %ERRORLEVEL%  NEQ 0  ( net share My=%SystemDrive%\Temp /GRANT:Users,FULL )
Change File permissions inside a folder
  • Applicable vista onwards.
  • DIR C:\Temp\WinTest-*.log 2> NUL 1> NUL & IF %ERRORLEVEL% EQU 0 FOR %I in (C:\Temp\WinTest-*.log) do ICACLS %I /grant EveryOne:(F)

There will definitely be Powershell, WMI and Vbscript way of doing same thing, but sometimes we do need CMD commands.