Solved

Stop and start a windows service on a remote host

Posted on 2004-09-10
2
717 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

860 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