Solved

Stop and start a windows service on a remote host

Posted on 2004-09-10
2
715 Views
Last Modified: 2008-03-03
I am trying to write a funtion that restarts specific services on a remote host - in VB.NET
I previously had a VBScript that simply used WinMgmts to perform this task & it still works fine, it's code is below. But I don't want my app to run external VBS files, nor WinMgmts. Any help would be great! Thanks.


Set objServiceFile = objFSO.OpenTextFile(serviceList, ForReading)
      varDictionaryCount = 0
      Do Until objServiceFile.AtEndOfStream
            strServiceLine = objServiceFile.Readline
            objServiceDictionary.Add varDictionaryCount, strServiceLine
            varDictionaryCount = varDictionaryCount + 1
      Loop
      objServiceFile.Close

      For Each objItem in objServiceDictionary
            strService = objServiceDictionary.Item(objItem)
            varToken = Split(strService)
            varServiceName = varToken(0)
            Wscript.Echo "Stopping the " & varToken(0) & " service."
            Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & varMachineName & "\root\cimv2")
            ProcErrorCheck
            Set colServiceList = objWMIService.ExecQuery ("SELECT * FROM win32_Service WHERE Name='" & varServiceName & "'")

            For Each objService In colServiceList
                  errReturn = objService.StopService()
                  If errReturn = 5 Then
                        Wscript.Echo "The " & varServiceName & " service was already stopped."
                  End If
            Next
      Next

      Wscript.Sleep 5000

      For Each objItem in objServiceDictionary
            strService = objServiceDictionary.Item(objItem)
            varTokens = Split(strService)
            varServiceName = varTokens(0)
            Wscript.Echo "Starting the " & varServiceName & " service."
            Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & varMachineName & "\root\cimv2")
            Set colServiceList = objWMIService.ExecQuery ("SELECT * FROM win32_Service WHERE Name='" & varServiceName & "'")

            For Each objService In colServiceList
                  errReturn = objService.StartService()
                  If errReturn <> 0 Then
                        Wscript.Echo "There was a problem starting the " & varServiceName & " service."
                  End If
            Next
      Next
      
      On Error GoTo 0
      Wscript.Echo "The restart services action has completed."
0
Comment
Question by:esdTeamsters
2 Comments
 
LVL 10

Accepted Solution

by:
heintalus earned 500 total points
ID: 12033355
Try this

Sub CheckServiceStatus(ByVal Servername As String, ByVal Service As String, ByVal status As String)
        Dim mycontroller As ServiceController
        Try
            mycontroller = New ServiceController(Service, Servername)
            Select Case status.ToLower
                Case "run"
                    If mycontroller.Status.ToString.ToLower = "stopped" Then
                        mycontroller.Start()
                    End If
                Case "stop"
                    If mycontroller.Status.ToString.ToLower = "running" Then
                        mycontroller.Stop()
                    End If
            End Select
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        Finally
            mycontroller.Dispose()
        End Try
    End Sub

You'll also need Imports System.ServiceProcess

HTH
Andy
0
 

Author Comment

by:esdTeamsters
ID: 12046680
Thanks a mil! This worked perfectly!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now