• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1775
  • Last Modified:

VBS Script to stop multiple services

I'm new to VBS and am trying to use the following script to stop these services but it's not working.  Can someone help with syntax. Thank you

servername = "localhost"
 set wmi = getobject("winmgmts://" & servername)
 StopService ("IMAService")
 StopService ("Spooler")
 StopService ("Independent Management Architecture")
 StopService ("IIS Admin Service")
 StopService ("Netlogon")
Sub StopService (ServiceName)
         wql = "select state from win32_service " _
               & "where displayname='"& ServiceName & "'"
         set results = wmi.execquery(wql)
         for each service in results
            if service.state = "Running" then
             service.stopService
            end if
          next
End Sub
0
Lou Pereira
Asked:
Lou Pereira
1 Solution
 
Lee W, MVPTechnology and Business Process AdvisorCommented:
If you are dead set on doing this with a VBS script, I can't help you.  But if you can deal with a batch script, this is easy.

net stop spooler
net stop netlogon
net stop iisadmin

Then you just need to get the correct service names for "IMAService" and "Independent Management Architecture" - which might be your VBS problem anyway.
0
 
vinnyd79Commented:
How about:


StopService ("IMAService")
StopService ("Spooler")
StopService ("Independent Management Architecture")
StopService ("IIS Admin Service")
StopService ("Netlogon")

Set WShell = Nothing
MsgBox "Done"

Sub StopService (ServiceName)
Set WShell = CreateObject("WScript.Shell")
WShell.Run "%COMSPEC% /C net stop " & ServiceName, 0, True
End Sub


Sub StartService (ServiceName)
Set WShell = CreateObject("WScript.Shell")
WShell.Run "%COMSPEC% /C net start " & ServiceName, 0, True
End Sub
0
 
sr75Commented:
You need to get the actual name of the service and not the display name of the service.  You are using the display name.  Here is a script to get the actual name of the service and its corresponding display name.

<<<script>>>

On Error Resume Next

Set FSO = CreateObject("Scripting.FileSystemObject")
Set colServices = objWMIService.ExecQuery("Select * from Win32_Service")

Set Logfile = FSO.OpenTextFile("Services.csv", 8 ,true)

For Each objService in colServices
            DName = objService.DisplayName
            RName = objService.Name
            SState = objService.State
            Logfile.writeline DName & ", " & RName & ", " & SState
Next

Logfile.close

<<<end script>>>

Now I always like to run small scripts so I would use an array like so:

<<<script>>>

On Error Resume Next

SVC = array("IMAService", _
                 "Spooler", _
                 "Independent Managment Architecture", _
                 "IIS Admin Servie", _
                 "Netlogon")

StrComputer = "."

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _
      & strComputer & "\root\cimv2")
x = 0

Do until x = 5
      Set colListOfServices = objWMIService.ExecQuery("Select * from" _
            & "Win32_Service Where Name ='" & SVC(x) & "'")

      For Each objService in colListOfServices
            objService.StartService()
      Next
      x = x + 1
loop

<<<end script>>>

Of course change the service names in the array to actual names provided by the previous script.
0
 
sr75Commented:
Sorry the display name script is missing two lines.  It should be this:

On Error Resume Next

StrComputer = "."

Set FSO = CreateObject("Scripting.FileSystemObject")

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _
     & strComputer & "\root\cimv2")

Set colServices = objWMIService.ExecQuery("Select * from Win32_Service")

Set Logfile = FSO.OpenTextFile("Services.csv", 8 ,true)

For Each objService in colServices
          DName = objService.DisplayName
          RName = objService.Name
          SState = objService.State
          Logfile.writeline DName & ", " & RName & ", " & SState
Next

Logfile.close
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now