Pause 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 pause methods on windows service using VB.Net. Here you can use "PauseService" Function which is given below, here you will pass two parameters one is Computer Name and other one is IP Address. Using this function you can perform this action on local or remote machine according to your need.
Private Sub PauseService(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

            objWMIService.Security_.ImpersonationLevel = 3

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

            For Each objService In colServiceList
                errReturn = objService.PauseService()
            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.PauseService()
                Next
            End If


            MessageBox.Show("Service Successfully Paused.", "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. You can pass Admin Credential as a parameter in Function also.
  • We used WMI Win32_Service for selecting particular service on desired machine.
  • Use For Loop for pausing that service.
  • Next step will be for pausing dependent services.
  • Here you have 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