batch file to remote stopping services

Home Forums Scripting General Scripting batch file to remote stopping services

Viewing 1 post (of 1 total)
  • Author
    Posts
  • Avatar
    pjeirke
    Member
    #113612

    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.

    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 [/CODE]

    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! :mrgreen:

    [COLOR=”Red”]MOD EDIT: Wrapped Code tags arround you’re script to make it more readable.[/COLOR][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 [/CODE]

    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! :mrgreen:

    MOD EDIT: Wrapped Code tags arround you’re script to make it more readable.

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.