-
Jun 17th, 2021, 11:07 AM
#1
Thread Starter
Fanatic Member
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!
-
Jun 17th, 2021, 11:19 AM
#2
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?
-
Jun 17th, 2021, 11:27 AM
#3
Thread Starter
Fanatic Member
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!
-
Jun 17th, 2021, 11:42 AM
#4
Re: Starting a Service Syntax
Originally Posted by ssabc
The service runs on another server.
OK.
Originally Posted by ssabc
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?
Originally Posted by ssabc
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.
-
Jun 17th, 2021, 12:40 PM
#5
Thread Starter
Fanatic Member
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!
-
Jun 17th, 2021, 04:31 PM
#6
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.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Jun 17th, 2021, 07:49 PM
#7
Re: Starting a Service Syntax
Originally Posted by ssabc
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.
-
Jun 18th, 2021, 08:58 AM
#8
Thread Starter
Fanatic Member
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!
-
Jun 18th, 2021, 09:12 AM
#9
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.
-
Jun 18th, 2021, 11:01 AM
#10
Thread Starter
Fanatic Member
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!
-
Jun 18th, 2021, 02:57 PM
#11
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|