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