Services - Get State

    Function funAreServicesRunning(sRemoteName As String) As String

        funAreServicesRunning = ""

        ListView1.SmallImageList = imgList

        'Try

        'Debug.Print("Computer = " & sRemoteName & "   " & newId.User.ToString)

        'subAddLogText("Computer = " & sRemoteName & "   " & newId.User.ToString)

        Debug.Print("Services for: " & sRemoteName)

        Try

            Dim services() As ServiceController = ServiceController.GetServices(sRemoteName)

            For Each lvItem As ListViewItem In ListView1.Items

                Try

                    For Each s As ServiceController In services

                        'Debug.Print("     " & s.ServiceName.ToString & " =? " & lvItem.SubItems(3).Text)

                        'Debug.Print("     " & LCase(sRemoteName) & " = " & LCase(lvItem.SubItems(2).Text))

                        If LCase(s.ServiceName.ToString) = LCase(lvItem.SubItems(3).Text) And LCase(sRemoteName) = LCase(lvItem.SubItems(2).Text) Then

                            Debug.Print("Found: " & s.Status.ToString)

                            Select Case s.Status

                                Case ServiceControllerStatus.Running

                                    lvItem.SubItems(0).Text = "Running"

                                    lvItem.ImageIndex = 0

                                Case ServiceControllerStatus.Stopped

                                    lvItem.SubItems(0).Text = "Stopped"

                                    lvItem.ImageIndex = 2

                                Case ServiceControllerStatus.StartPending

                                    lvItem.SubItems(0).Text = "Start Pending"

                                    lvItem.ImageIndex = 1

                                Case ServiceControllerStatus.StopPending

                                    lvItem.SubItems(0).Text = "StopPending"

                                    lvItem.ImageIndex = 2

                                Case Else

                                    lvItem.SubItems(0).Text = "Unknown"

                                    lvItem.ImageIndex = 2

                            End Select

                            Exit For

                        End If

                        'If (s.Status <> ServiceControllerStatus.Running) Then

                        '    For Each sService In aServicesToMonitor

                        '        If sService = s.ServiceName.ToString Then

                        '            Debug.Print("Stopped: " & s.ServiceName.ToString)

                        '            funAreServicesRunning += sService & ","

                        '            subAddLogText("   Stopped: " & s.ServiceName.ToString)

                        '        End If

                        '    Next

                        'End If

                    Next

                Catch ex As Exception

                    lvItem.SubItems(0).Text = "Unknown"

                    lvItem.ImageIndex = 2

                End Try

            Next

        Catch ex As Exception

            Debug.Print("EXCEPT: " & ex.Message.ToString)

        End Try

    End Function

Stop

 Private Sub subStop(lvItem As ListViewItem)

        'If ListView1.CheckedItems.Count > 0 Then

        'subImpersonate()

        'For Each lvItem As ListViewItem In ListView1.CheckedItems

        lvItem.ImageIndex = 0

        lvItem.SubItems(0).Text = Threading.Thread.CurrentThread.ManagedThreadId.ToString

        Debug.Print("Services from btnStop: " & lvItem.SubItems(2).Text)

            Debug.Print("Services from btnStop: " & lvItem.SubItems(1).Text)

        Dim services() As ServiceController

        Try

            services = ServiceController.GetServices(lvItem.SubItems(2).Text)

            For Each s As ServiceController In services

                'Debug.Print("     " & s.ServiceName.ToString & " =? " & lvItem.SubItems(3).Text)

                If LCase(s.ServiceName.ToString) = LCase(lvItem.SubItems(3).Text) Then

                    Debug.Print("Found: " & s.Status.ToString)

                    Debug.Print("Stopping")

                    Form1.TextBox1.AppendText("STOPPING" & vbCrLf)

                    Select Case s.Status

                        Case ServiceControllerStatus.Running

                            lvItem.ImageIndex = 0

                            s.Stop()

                            s.WaitForStatus(ServiceControllerStatus.Stopped, New TimeSpan(0, 0, 2, 0, 0))

                            If ServiceControllerStatus.Stopped Then

                                lvItem.ImageIndex = 2

                                lvItem.SubItems(0).Text = "Stopped"

                                lvItem.Checked = False

                            Else

                                lvItem.ImageIndex = 1

                            End If

                            Exit For

                    End Select

                End If

            Next

        Catch ex As Exception

            lvItem.Text = "stop err"

        End Try

    End Sub


Start

    Private Sub subStart(lvItem As ListViewItem)

        lvItem.ImageIndex = 0

        lvItem.SubItems(0).Text = Threading.Thread.CurrentThread.ManagedThreadId.ToString

        Debug.Print("Services from btnStart: " & lvItem.SubItems(2).Text)

        Dim services() As ServiceController

        Try

            services = ServiceController.GetServices(lvItem.SubItems(2).Text)

            For Each s As ServiceController In services

                'Debug.Print("     " & s.ServiceName.ToString & " =? " & lvItem.SubItems(3).Text)

                If LCase(s.ServiceName.ToString) = LCase(lvItem.SubItems(3).Text) Then

                    Debug.Print("Found: " & s.Status.ToString)

                    Debug.Print("Starting")

                    Select Case s.Status

                        Case ServiceControllerStatus.Stopped

                            lvItem.ImageIndex = 0

                            s.Start()

                            s.WaitForStatus(ServiceControllerStatus.Running, New TimeSpan(0, 0, 2, 0, 0))

                            If ServiceControllerStatus.Running Then

                                lvItem.ImageIndex = 0

                                lvItem.SubItems(0).Text = "Running"

                                lvItem.Checked = False

                            Else

                                lvItem.ImageIndex = 1

                            End If

                            Exit For

                    End Select

                End If

            Next

        Catch ex As Exception

            lvItem.Text = "start err"

        End Try

    End Sub

Getting Service Start type

Dim st As Object

st = s

Debug.Print(s.DisplayName & " = " & s.Status.ToString & " " & st.StartType.ToString)

Passing arguments to a Windows Service

According to this answer and the comments, the args parameter of OnStart is only used when manually setting start parameters in the windows service dialog, which cannot be saved.

You can use the arguments you are setting up by accessing them in the Main method (located in the Service.Designer.vb file by default). Below is an example:

<MTAThread()> _
<System.Diagnostics.DebuggerNonUserCode()> _
Shared Sub Main(ByVal args As String())
    Dim ServicesToRun() As System.ServiceProcess.ServiceBase
    ServicesToRun = New System.ServiceProcess.ServiceBase() {New Service1(args)}
    System.ServiceProcess.ServiceBase.Run(ServicesToRun)
End Sub

You will need to add or modify a constructor to your service class to accept the arguments:

Private ReadOnly _arguments As String()

Public Sub New(ByVal args As String())
    InitializeComponent()
    _arguments = args
End Sub

Then your OnStart method becomes:

Protected Overrides Sub OnStart(ByVal args() As String)
    If Not IsNothing(args) Then
        Library.WriteLog("Number of args = " & _arguments.Count)
        If args.Count > 0 Then
            For i = 0 To args.Count - 1
                Library.WriteLog("Arg" & i & ": " & _arguments(i))
            Next
        End If
    End If
End Sub


https://stackoverflow.com/questions/30045201/create-service-with-arguments-using-sc-exe

Get Service ImagePath and/or Display Name

    Function FindImagePathDetailsforServer(sRemoteName As String, sServiceName As String, sProperty As String, Optional bMatchDisplayNameToo As Boolean = False) As String
        ' Dim sRemoteName As String = args(0)
        subImpersonate()
        Dim sReturnValue As String = ""

        Dim oReg As RegistryKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, sRemoteName)

        Dim sBaseKey As String = "SYSTEM\CurrentControlSet\Services"

        For Each oValue As String In oReg.OpenSubKey(sBaseKey).GetSubKeyNames
            Dim oPath As RegistryKey = oReg.OpenSubKey(sBaseKey & "\" & oValue)
            If LCase(oValue) = LCase(sServiceName) Or IIf(bMatchDisplayNameToo, CBool(LCase(sServiceName) = LCase(oPath.GetValue("DisplayName"))), False) Then
                'Console.WriteLine("oValue: " & oValue)

                'Console.WriteLine("   " & oPath.GetValue("DisplayName"))
                'Console.Write("   " & oPath.GetValue("ImagePath") & "  ->  ")
                Select Case LCase(sProperty)
                    Case "imagepath"
                        Try
                            sReturnValue = (IO.Path.GetFileName(Replace(oPath.GetValue("ImagePath"), Chr(34), "")))

                        Catch ex As Exception
                        Finally
                            'Console.WriteLine()

                        End Try
                    Case Else
                        sReturnValue = oPath.GetValue(sProperty)
                End Select

                'Console.WriteLine("   " & oPath.GetValue("ObjectName"))
            End If

            'Dim oPath As String = oReg.OpenSubKey("Software\Lemmermann\MEDENTChadCantDoItHa\Printers\" & oValue).GetValue("PrintQueuesToMonitor")
        Next
        subCancelImpersonate()


        'Dim services() As ServiceController = ServiceController.GetServices(sRemoteName)

        'For Each s As ServiceController In services

        '    Dim st As Object
        '    st = s
        'Next
        Return sReturnValue

    End Function