Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Stop and start a windows service on a remote host

Posted on 2004-09-10
2
Medium Priority
?
724 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 10

Accepted Solution

by:
heintalus earned 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

610 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