Start Service on Remote Machine

What is Windows Service

Windows Service is a background process that performs certain task. We can perform various methods on service like start,stop,pause or resume on local or remote machine.

In this article you will perform resume methods on service using VB.Net. Here you can use "ResumeService" Function which is given below. Using this function you can perform this action on local or remote machine according to your need.
Private Sub StartService(ByVal strComputer As String, ByVal strService As String)

        Try

            Dim objWMIService, colServiceList, objService, errReturn, objSWbemLocator

            objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")

            If (strComputer = Environment.MachineName.ToString) Then

                objWMIService = objSWbemLocator.ConnectServer(strComputer, "root\cimv2")
            Else
                objWMIService = objSWbemLocator.ConnectServer(strComputer, "root\cimv2", UserName, Password)
            End If

            colServiceList = objWMIService.ExecQuery("Select * from Win32_Service where DisplayName='" & strService & "'")

            For Each objService In colServiceList
                errReturn = objService.StartService()
            Next

            If (errReturn = 0) Then
                colServiceList = objWMIService.ExecQuery("Associators of " & "{Win32_Service.DisplayName='" & strService & "'} Where " & "AssocClass=Win32_DependentService " & "Role=Dependent")

                For Each objService In colServiceList
                    objService.StartService()
                Next
            End If

            MessageBox.Show("Service Successfully Started.", "Service Manager", MessageBoxButtons.OK, MessageBoxIcon.Information)

        Catch ex As Exception

            MessageBox.Show(ex.Message, "Service Manager", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
        
End Sub
Step-By-Step
  • Create object for connecting server either local or remote.
  • For Remote connection we use admin credential.
  • Use WMI Win32_Service for selecting particular service on desired machine.
  • Use For Loop for resuming that service.
  • Next step will be for resuming dependent services.
  • Successfully done.

Related Posts by Categories



Stumble This Fav This With Technorati Add To Del.icio.us Digg This Add To Reddit Add To Facebook Add To Yahoo

0 comments:

Post a Comment