Check status, start or stop a remote Windows Service with vb.net

Good Morning. It was a pretty laid back night. Did a lot of traveling yesterday so I was pretty tired. The temperature is getting cold again but what you gonna do? I live in Wisconsin after all. My daughter got to see High School Musical III last night so she is happy. And that is a good thing.

Well the title pretty much says it all doesn’t it? But a couple of neat things to point out. We had situations with IIS where it would it would stop running and we needed to check it is how this came to be. I am using WMI and terminal services because I want to connect to a remote PC and the native .NET methods were cumbersome for me to use. Also, there is no native way to shut down a Windows Service at least that I could find. So I change the startup type of the service. Not tested on anything but Windows Server 2003! Anyway, hope it is helpful and hope you have a great day!


Join me on Facebook

Imports System.ServiceProcess
Imports System.Management
 
Public Class TS_Service
 
    ' Is the service running?
    ' Returns true if we need to start the service
    Public Function CheckService(ByVal PC As String) As Boolean
        Dim obj As ManagementObject
 
        obj = New ManagementObject("\\" & PC & "\root\cimv2:Win32_Service.Name='TermService'")
        If Not IsNothing(obj) Then
            If obj("StartMode").ToString <> "Disabled" And obj("State").ToString = "Running" Then
                Return False
            End If
        End If
        Return True
    End Function
 
    Public Sub StartService(ByVal PC As String)
        Dim obj As ManagementObject
        Dim inParams, outParams As ManagementBaseObject
        Dim Result As Integer
        Dim sc As ServiceController
 
        obj = New ManagementObject("\\" & PC & "\root\cimv2:Win32_Service.Name='TermService'")
 
        ' Change the Start Mode to Manual
        If obj("StartMode").ToString = "Disabled" Then
            ' Get an input parameters object for this method
            inParams = obj.GetMethodParameters("ChangeStartMode")
            inParams("StartMode") = "Manual"
 
            ' do it!
            outParams = obj.InvokeMethod("ChangeStartMode", inParams, Nothing)
            Result = Convert.ToInt32(outParams("returnValue"))
            If Result <> 0 Then
                Throw New Exception("ChangeStartMode method error code " & Result)
            End If
        End If
 
        ' Start the service
        If obj("State").ToString <> "Running" Then
            ' now start the service
            sc = New ServiceController("TermService", PC)
            sc.Start()
            sc.WaitForStatus(ServiceControllerStatus.Running)
        End If
    End Sub
 
    Public Sub StopService(ByVal PC As String)
        Dim obj As ManagementObject
        Dim inParams, outParams As ManagementBaseObject
        Dim Result As Integer
 
        
        ' we can only disable the service and wait
        ' until the next reboot to stop it
 
         
        obj = New ManagementObject("\\" & PC & "\root\cimv2:Win32_Service.Name='TermService'")
 
        ' change the Start Mode to Disabled
        If obj("StartMode").ToString <> "Disabled" Then
            ' Get an input parameters object for this method
            inParams = obj.GetMethodParameters("ChangeStartMode")
            inParams("StartMode") = "Disabled"
 
            ' do it!
            outParams = obj.InvokeMethod("ChangeStartMode", inParams, Nothing)
            Result = Convert.ToInt32(outParams("returnValue"))
            If Result <> 0 Then
                Throw New Exception("ChangeStartMode method error code " & Result)
            End If
        End If
    End Sub
End Class
 
Advertisements
  1. #1 by Unknown on January 27, 2009 - 5:36 pm

    Hello,First, thank you for posting this. I have been trying to do this for a while but I am not sure how to get the authentication working. For example, I am trying to run this program on my local machine and check status of a Windows 2003 server service (spooler, termserver, etc). I get an "Access Denied" exception. I would appreciate any pointers/direction you can provide.

  2. #2 by m on July 31, 2009 - 1:45 pm

    Imports System.ServiceProcessWarning 1 Namespace or type specified in the Imports \’System.ServiceProcess\’ doesn\’t contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn\’t use any aliases. 9 RemoteService

  3. #3 by Jim M on May 16, 2012 - 9:55 am

    You could use the System.ServiceProcess namespace for this to simplify your code.
    Dim TermService As New _
    System.ServiceProcess.ServiceController(“TermService”, “MachineName”)
    TermService.Start()
    TermService.Stop()

  4. #4 by Ishant on April 29, 2014 - 9:23 am

    Hi, Thanks for such a useful code. I have used the function of Start Service and created dll for the same. I have registered the dll and now i am using it in QTP but it gives me an error saying can’t create object. Could you please help?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: