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!

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

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.

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

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()

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?