beardmann

Stop Service Remote Nice

Apr 21st, 2015
6,069
0
Never
2
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @echo off
  2.  
  3. IF [%1]==[] GOTO usage
  4. IF [%2]==[] GOTO usage
  5.  
  6. ping -n 1 %1 | FIND "TTL=" >NUL
  7. IF errorlevel 1 GOTO SystemOffline
  8. SC \\%1 query %2 | FIND "STATE" >NUL
  9. IF errorlevel 1 GOTO SystemOffline
  10.  
  11. :ResolveInitialState
  12. SC \\%1 query %2 | FIND "STATE" | FIND "RUNNING" >NUL
  13. IF errorlevel 0 IF NOT errorlevel 1 GOTO StopService
  14. SC \\%1 query %2 | FIND "STATE" | FIND "STOPPED" >NUL
  15. IF errorlevel 0 IF NOT errorlevel 1 GOTO StopedService
  16. SC \\%1 query %2 | FIND "STATE" | FIND "PAUSED" >NUL
  17. IF errorlevel 0 IF NOT errorlevel 1 GOTO SystemOffline
  18. echo Service State is changing, waiting for service to resolve its state before making changes
  19. sc \\%1 query %2 | Find "STATE"
  20. timeout /t 2 /nobreak >NUL
  21. GOTO ResolveInitialState
  22.  
  23. :StopService
  24. echo Stopping %2 on \\%1
  25. sc \\%1 stop %2 %3 >NUL
  26.  
  27. GOTO StopingService
  28. :StopingServiceDelay
  29. echo Waiting for %2 to stop
  30. timeout /t 2 /nobreak >NUL
  31. :StopingService
  32. SC \\%1 query %2 | FIND "STATE" | FIND "STOPPED" >NUL
  33. IF errorlevel 1 GOTO StopingServiceDelay
  34.  
  35. :StopedService
  36. echo %2 on \\%1 is stopped
  37. GOTO:eof
  38.  
  39. :SystemOffline
  40. echo Server \\%1 or service %2 is not accessible or is offline
  41. GOTO:eof
  42.  
  43. :usage
  44. echo Will cause a remote service to STOP (if not already stopped).
  45. echo This script will waiting for the service to enter the stopped state if necessary.
  46. echo.
  47. echo %0 [system name] [service name] {reason}
  48. echo Example: %0 server1 MyService
  49. echo.
  50. echo For reason codes, run "sc stop"
  51. GOTO:eof
Advertisement