Solved

Stop and start a windows service on a remote host

Posted on 2004-09-10
2
718 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

Title # Comments Views Activity
.NET 2008 VB and C# 6 48
Groupbox Control ? 2 27
VB.net -  GroupBox / Panel DataBinding and behavior 3 23
VB.NET Inline If statement 4 40
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
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…

749 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