Announcement

Collapse
No announcement yet.

Start services that didnít started automatically

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

  • Start services that didnít started automatically

    Hi,


    I have an issue when I work with Hyper-V servers that in some cases services fails to start automatically.
    I have a script that list given service names and it will show the state.
    What I would like is a script that will run and start all services that didnít started automatically

    Code:
    ArrComputer = Array(".")
    ArrServices = Array("Service1", "Service2", "Service3")
    i = 0
    For Each strComputer In ArrComputer        
    	Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")        
    	For Each Service In ArrServices            
    		If i > 0 Then                
    			str = str & " or DisplayName = '" & Service & "'"            
    		Else                
    			str = "DisplayName = '" & Service & "'"                
    			i = i + 1            
    		End If        
    	Next        
    	Set colItems = objWMIService.ExecQuery("Select DisplayName, State from Win32_Service where " & str)        
    	For Each objItem in colItems            
    		Msgbox ("DisplayName: "  & objItem.DisplayName  & vbNewLine & " " & vbNewLine & "State: "  & objItem.State & VbCrLf   )
    	Next
    Next

    Thanks

  • #2
    Re: Start services that didnít started automatically

    Source: http://www.computerperformance.co.uk...i_services.htm
    Code:
    Option Explicit
    Dim objWMIService, objItem, objService
    Dim colListOfServices, strComputer, strService, intSleep 
    strComputer = "."
    intSleep = 180000
    
    'On Error Resume Next
    ' NB strService is case sensitive.
    strService = " 'Alerter' "
    Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
    & strComputer & "\root\cimv2")
    Set colListOfServices = objWMIService.ExecQuery _
    ("Select * from Win32_Service Where Name ="_
    & strService & " ")
    For Each objService in colListOfServices
    objService.StopService()
    WSCript.Sleep intSleep
    objService.StartService()
    Next 
    WScript.Echo "Your "& strService & " service has Started" 
    WScript.Quit
    ' End of Example WMI script to Start / Stop services

    I personally do the following via a script: (not shown here, I'm at home)
    1) Check an Array of services
    2) Feed the array into a Procedure (like the code above)
    3) Sleep the script for 180 seconds (to give plenty of time for most services)
    4) Loop back and check again
    5) Any services that failed the restart are emailed to me so I can login and find out why.

    Hobie

    Comment

    Working...
    X