Results 1 to 11 of 11

Thread: Starting a Service Syntax

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2013
    Location
    Minneapolis, MN
    Posts
    531

    Starting a Service Syntax

    Hello:

    In looking at this, I want to set a service to run. I see nothing in this reference for the correct object syntax options.

    https://docs.microsoft.com/en-us/dot...System_Object_


    I have this code, which I really believe should work, if only I knew what the syntax was to Start or Restart the service...

    Code:
    service.SetPropertyValue("Name", "Running")

    Code:
        Private Sub StartService(ByVal _service As String)
            Dim connection As System.Management.ConnectionOptions = New ConnectionOptions()
            connection.Username = "Administrator"
            connection.Password = "hidden"
            connection.Authority = "ntlmdomain:WACONIAMFG"
            connection.EnablePrivileges = True
            connection.Authentication = AuthenticationLevel.[Default]
            connection.Impersonation = ImpersonationLevel.Impersonate
            Dim scope As ManagementScope = New ManagementScope("\\CADSERV1\root\CIMV2", connection)
            scope.Connect()
    
            Dim path As ManagementPath = New ManagementPath("Win32_Service")
            Dim services As ManagementClass = New ManagementClass(scope, path, Nothing)
    
            For Each service As ManagementObject In services.GetInstances()
                If service.GetPropertyValue("Name") = _service Then
                    service.SetPropertyValue("Name", "Running")
                    Exit For
    
                End If
    
            Next
    
        End Sub
    Last edited by ssabc; Jun 17th, 2021 at 11:26 AM.
    - A 'Hyperactive Member' trying to make a difference in a hyperactive world! And recently, I've been promoted to a 'Finatic Member,' whatever that means!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Starting a Service Syntax

    Is there a particular reason that you went down the more complex path of WMI rather than using the ServiceController class?

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2013
    Location
    Minneapolis, MN
    Posts
    531

    Re: Starting a Service Syntax

    The service runs on another server. I assume what you are referring to is what I should research? Is there no syntax to start or restart a service?
    - A 'Hyperactive Member' trying to make a difference in a hyperactive world! And recently, I've been promoted to a 'Finatic Member,' whatever that means!

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Starting a Service Syntax

    Quote Originally Posted by ssabc View Post
    The service runs on another server.
    OK.
    Quote Originally Posted by ssabc View Post
    I assume what you are referring to is what I should research?
    I wouldn't have mentioned it if I didn't think it wasn't applicable. The name kind of gives it away. What do you suppose a service controller does?
    Quote Originally Posted by ssabc View Post
    Is there no syntax to start or restart a service?
    Why are you asking me when you could just read the relevant documentation? ALWAYS read the documentation first, then ask questions later if and only if you need to.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2013
    Location
    Minneapolis, MN
    Posts
    531

    Re: Starting a Service Syntax

    Sure!

    I did research it on Google as I always do. This is how I came up with what I currently have.
    - A 'Hyperactive Member' trying to make a difference in a hyperactive world! And recently, I've been promoted to a 'Finatic Member,' whatever that means!

  6. #6
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,597

    Re: Starting a Service Syntax

    Don't use WMI when not needed.
    You can start a service with, as JMC, suggested, ServiceController.
    This WILL run a remote service start but you might need windows impersonation to bypass any security issues.
    See here on post no7 : https://www.vbforums.com/showthread....(User-folders)
    Note you might need to change ("username", "domain", "password", 3, 0, tokenHandle) to ("username", "domain", "password", 9, 0, tokenHandle), if it does not work.
    Apparently WMI will also start -- stop a service but it will be an overhead so just use ServiceController with impersonation.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Starting a Service Syntax

    Quote Originally Posted by ssabc View Post
    Sure!

    I did research it on Google as I always do. This is how I came up with what I currently have.
    Regardless of what you did before, I have now provided you with the name of a type that you apparently didn't know about before. When you learn about a new type or member, the very first thing you should do is read the documentation for that type or member. It's not something someone should have to suggest or instruct. You should just do it as a matter of course, then look elsewhere if you still need more information. RTFM is a cliché for a reason.

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2013
    Location
    Minneapolis, MN
    Posts
    531

    Re: Starting a Service Syntax

    jmc:

    https://docs.microsoft.com/en-us/dot...t-plat-ext-5.0

    Service Controller seems like a nice technology. However, I cannot find one example that tells me how to enter my server and credentials to see the services on a remote server. On the other hand, there are examples with WMI.

    Apparently, this information is a huge secret.

    Also, as I've explained before, I am not a developer, at least not 100% of the time. I do what needs to be done to support a manufacturing company. This is why I always tell you, you seem very hard on me with your responses. We have a service that keeps getting knocked out about once a week, probably due to a Windows Update, and we want specific users to be able to start these services, potentially at 4:30 AM, so we can keep manufacturing going. Our goal is to meet deadlines and make money, and quite honestly, any working technology to meet this need is acceptable.
    - A 'Hyperactive Member' trying to make a difference in a hyperactive world! And recently, I've been promoted to a 'Finatic Member,' whatever that means!

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Starting a Service Syntax

    Perhaps the ServiceController class only lets you connect to a remote machine as the current user. It's not something that I've ever done myself. When I asked whether there was a reason you weren't using it, that was actually a genuine question. It looks like that could be the reason.

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2013
    Location
    Minneapolis, MN
    Posts
    531

    Re: Starting a Service Syntax

    Thanks.

    Good news is, I got it working using WCF.
    - A 'Hyperactive Member' trying to make a difference in a hyperactive world! And recently, I've been promoted to a 'Finatic Member,' whatever that means!

  11. #11
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,597

    Re: Starting a Service Syntax

    If you use impersonation it will work.
    I have done it so but since you seem to ignore me go ahead and use WMI. Note if a PC has WMI provider disable you won't get it to work.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width