Solved

Stop and start a windows service on a remote host

Posted on 2004-09-10
2
714 Views
Last Modified: 2008-03-03
I am trying to write a funtion that restarts specific services on a remote host - in VB.NET
I previously had a VBScript that simply used WinMgmts to perform this task & it still works fine, it's code is below. But I don't want my app to run external VBS files, nor WinMgmts. Any help would be great! Thanks.


Set objServiceFile = objFSO.OpenTextFile(serviceList, ForReading)
      varDictionaryCount = 0
      Do Until objServiceFile.AtEndOfStream
            strServiceLine = objServiceFile.Readline
            objServiceDictionary.Add varDictionaryCount, strServiceLine
            varDictionaryCount = varDictionaryCount + 1
      Loop
      objServiceFile.Close

      For Each objItem in objServiceDictionary
            strService = objServiceDictionary.Item(objItem)
            varToken = Split(strService)
            varServiceName = varToken(0)
            Wscript.Echo "Stopping the " & varToken(0) & " service."
            Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & varMachineName & "\root\cimv2")
            ProcErrorCheck
            Set colServiceList = objWMIService.ExecQuery ("SELECT * FROM win32_Service WHERE Name='" & varServiceName & "'")

            For Each objService In colServiceList
                  errReturn = objService.StopService()
                  If errReturn = 5 Then
                        Wscript.Echo "The " & varServiceName & " service was already stopped."
                  End If
            Next
      Next

      Wscript.Sleep 5000

      For Each objItem in objServiceDictionary
            strService = objServiceDictionary.Item(objItem)
            varTokens = Split(strService)
            varServiceName = varTokens(0)
            Wscript.Echo "Starting the " & varServiceName & " service."
            Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & varMachineName & "\root\cimv2")
            Set colServiceList = objWMIService.ExecQuery ("SELECT * FROM win32_Service WHERE Name='" & varServiceName & "'")

            For Each objService In colServiceList
                  errReturn = objService.StartService()
                  If errReturn <> 0 Then
                        Wscript.Echo "There was a problem starting the " & varServiceName & " service."
                  End If
            Next
      Next
      
      On Error GoTo 0
      Wscript.Echo "The restart services action has completed."
0
Comment
Question by:esdTeamsters
2 Comments
 
LVL 10

Accepted Solution

by:
heintalus earned 500 total points
ID: 12033355
Try this

Sub CheckServiceStatus(ByVal Servername As String, ByVal Service As String, ByVal status As String)
        Dim mycontroller As ServiceController
        Try
            mycontroller = New ServiceController(Service, Servername)
            Select Case status.ToLower
                Case "run"
                    If mycontroller.Status.ToString.ToLower = "stopped" Then
                        mycontroller.Start()
                    End If
                Case "stop"
                    If mycontroller.Status.ToString.ToLower = "running" Then
                        mycontroller.Stop()
                    End If
            End Select
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        Finally
            mycontroller.Dispose()
        End Try
    End Sub

You'll also need Imports System.ServiceProcess

HTH
Andy
0
 

Author Comment

by:esdTeamsters
ID: 12046680
Thanks a mil! This worked perfectly!
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now