Announcement

Collapse
No announcement yet.

batch file to remote stopping services

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • batch file to remote stopping services

    Hi Guys,

    I'm having some trouble figuring out how this could work.
    The problem is:
    I need to stop certain services in order on 5 servers.
    These services are running under a special account and only those need to stop.

    this is what I got so far.

    Code:
    strComputer = "server1" 
    Set WMI = GetObject("winmgmts:\\" & strComputer) 
    
    services_list = "service1, service2, service3" 
    services_array = Split(services_list,",") 
    
    
    for i = 0 to UBound(services_array) 
      servicename = Trim(services_array(i)) 
      wql = "select state from win32_service " _ 
          & "where displayname='" & servicename & "'" 
      set results = wmi.execquery(wql) 
      for each service in results 
        WScript.Echo servicename & ": " & service.state 
        if service.state = "Running" then 
          service.stopService 
          do 
            WScript.Sleep 300 
            wql = "select state from win32_service " _ 
                & "where displayname='" & servicename & "'" 
            set results2 = wmi.execquery(wql) 
            for each service2 in results2 
              if service2.state = "Stopped" Then 
                WScript.Echo servicename & ": " & service2.state 
                Stopped = True 
              end if 
            next 
          loop until Stopped 
        end if 
      next 
    next
    this would be for server 1, trying to stop first service 1 then service 2.
    What need to be added! The same script for 5 servers and only services running under domain/user1.

    Is this possible?
    Anyone that can fix this will get a night of free beers if they get to Antwerp!

    MOD EDIT: Wrapped Code tags arround you're script to make it more readable.
    Last edited by Dumber; 8th July 2006, 17:05.

  • #2
    Re: batch file to remote stopping services

    You should create a loop for the computernames..
    For example (i can't test it currently)
    It's just a thought, but sure, there would be better and faster options.
    Code:
    Dim StrComputer, Counter, computer
    Counter = "0"
    Do while Counter <> "4"
    
    If counter =0 Then Strcomputer = server 1
    If counter =1 Then Strcomputer = server 2
    If counter =2 Then Strcomputer = server 3
    If counter =3 Then Strcomputer = server 4
    If counter =4 Then Strcomputer = server 5
    
     Set WMI = GetObject("winmgmts:\\" & strComputer) 
     StopService (StrComputer)
    loop
    
    Function StopService (StrComputer)
      services_list = "service1, service2, service3" 
      services_array = Split(services_list,",") 
    
    for i = 0 to UBound(services_array) 
      servicename = Trim(services_array(i)) 
      wql = "select state from win32_service " _ 
          & "where displayname='" & servicename & "'" 
      set results = wmi.execquery(wql) 
      for each service in results 
        WScript.Echo servicename & ": " & service.state 
        if service.state = "Running" then 
          service.stopService 
          do 
            WScript.Sleep 300 
            wql = "select state from win32_service " _ 
                & "where displayname='" & servicename & "'" 
            set results2 = wmi.execquery(wql) 
            for each service2 in results2 
              if service2.state = "Stopped" Then 
                WScript.Echo servicename & ": " & service2.state 
                Stopped = True 
              end if 
            next 
          loop until Stopped 
        end if 
      next 
    next 
    
    Counter =Counter +1
    End Function
    but another, more easier option could be psservice, which you could run from a Batch file
    http://www.sysinternals.com/Utilities/PsService.html
    Last edited by Dumber; 8th July 2006, 17:07.
    Marcel
    Technical Consultant
    Netherlands
    http://www.phetios.com
    http://blog.nessus.nl

    MCITP(EA, SA), MCSA/E 2003:Security, CCNA, SNAF, DCUCI, CCSA/E/E+ (R60), VCP4/5, NCDA, NCIE - SAN, NCIE - BR, EMCPE
    "No matter how secure, there is always the human factor."

    "Enjoy life today, tomorrow may never come."
    "If you're going through hell, keep going. ~Winston Churchill"

    Comment


    • #3
      Re: batch file to remote stopping services

      Marcel,

      thank you (bedankt) for replying.

      Can I use pservice to script?

      I need to check the status of an other script before stopping another!

      Thx,
      Pjeirke

      Comment

      Working...
      X