|
-
May 31st, 2007, 03:25 AM
#1
Thread Starter
New Member
How to access an Service on a Remote PC (Start/Stop it!!!)
Hope this is the right place to ask.
I have a Sercive running on one PC. I need to be able to point this PC out from a VB .NET program running on another PC and with this VB program be able to start and stop the Service on the remote PC.
I have tested the program on with the Service running on the same PC as the VB program, and with this constellation it is possible to start and stop the service. But if I try and move the Service to a remote PC and the run the VB program again, I get an error.
From the PC with the VB program there are access to the remote PC, I am able to copy files between the two PC’s and I can look into a SQL database on the remote PC too.
I have looked around and it seems like there is other with the same problem, but I have not been able to find a solution any ware. Also the Microsoft documentation does not give any answer to this problem.
Peter
Code:
Imports System
Imports System.ServiceProcess
Module Module_Services
Public Sub StartService()
Dim DataSource As String = "MachineName"
Dim myServiceName As String = "Service"
Dim sStatus As String
Dim myController As ServiceController
myController = New ServiceController
myController.MachineName = DataSource
myController.ServiceName = myServiceName
Try
myController.Refresh()
sStatus = myController.Status.ToString
myController.Start()
Catch exp As Exception
MsgBox("Could not start service")
End Try
End Sub
End Module
Last edited by Peter01; Jun 5th, 2007 at 10:29 AM.
Reason: Realy need to know!!
-
May 31st, 2007, 05:32 AM
#2
Re: How to access an Service on a Remote PC
 Originally Posted by Peter01
if I try and move the Service to a remote PC and the run the VB program again, I get an error.
If you get an error then I would think that you get an error message. Care to share?
-
May 31st, 2007, 07:37 AM
#3
Thread Starter
New Member
Re: How to access an Service on a Remote PC
The error is:
Code:
InvalidOperationException was unhandled
Cannot open Service Control manager on computer 'MachineName'. This operation might require other privileges.
Peter
-
May 31st, 2007, 06:32 PM
#4
Re: How to access an Service on a Remote PC
You've got an unhandled exception. The first thing to do is to handle it to see if you can get any further information about it, e.g.
vb.net Code:
Try
'Put your old code here.
Catch ex As Exception
MessageBox.Show(ex.ToString())
End Try
You can do more than that too but it's a good place to start.
-
Jun 1st, 2007, 01:30 AM
#5
Thread Starter
New Member
Re: How to access an Service on a Remote PC
Hi jmcilhinney
I Changed the Code as advised and now I get the following informaton:
First a small message boc comes up telling me:
Could not start service
Then:
System.InvalidOperationException: Cannot open Service control Manager on computer 'MachineName'. This operation might require other privileges. --> System.ComponentModel.Win32Exception: The RPC server is unavailable
-- End of inner exception stack trace --
at System.ServerProcess.ServiceController.GetDataBaseHandleWothAccess(String machineName, Int32 serviceControlManagerAccess)
at System.ServerProcess.ServiceController.GetDataBaseHandleWithConnectAccess()
at System.ServerProcess.ServiceController.GetServiceHandle(Int32 desiredAccess)
at System.ServerProcess.ServiceController.GenerateStatus()
at System.ServerProcess.ServiceController..get_Status()
at LogDataFlowModule_Services.StartService() in C:\Folder\Module_Service.vb:line34
-
Jun 1st, 2007, 01:38 AM
#6
Re: How to access an Service on a Remote PC
Seems like a permission issue to me. Do you have administrative privileges? Are you logging on to a Domain?
Use [code] source code here[/code] tags when you post source code.
My Articles
-
Jun 1st, 2007, 02:21 AM
#7
Fanatic Member
Re: How to access an Service on a Remote PC
You did change the value of DataSource from "MachineName" to the name of the machine you want to scan didn't you?
ManagePC - the all-in-one PC management and inventory tool
-
Jun 1st, 2007, 06:35 AM
#8
Thread Starter
New Member
Re: How to access an Service on a Remote PC
Yes for me too it lookes like I do not have permission to access the remote PC called "MachineName".
The Account and Password on the PC from where I run the VB program are not existing on the Remote PC, but I have Username and Password to this machine, and if there are any places where to key this information in, in the VB code I can do that.
The Remote PC is not in a Domain, it is in the default "workgroup".
For me it looks like I am missing some Code making the access to the remote PC. But I have not been able to locate any information about how to do. The only thing that I have found is that in the Microsoft documentation is that the "MachineName" has to be changed to the name of the Remote PC, but there are no examples and just changing the name do not work.
Peter
-
Jun 5th, 2007, 10:28 AM
#9
Thread Starter
New Member
Re: How to access an Service on a Remote PC
Are there realy nobody that konws how to do this?
Peter
-
Jun 5th, 2007, 11:38 PM
#10
Frenzied Member
Re: How to access an Service on a Remote PC (Start/Stop it!!!)
Hello Peter,
http://msdn2.microsoft.com/en-us/lib...8VS.71%29.aspx
Here is an article on how to commuicate with a windows service. Not sure if it is what you want.
I would be interested in a solution to this problem, if you ever find one.
-
Jun 6th, 2007, 03:07 AM
#11
Re: How to access an Service on a Remote PC (Start/Stop it!!!)
If the machine were in a network you could have done an "impersonation"
Now you say workgroup, so i'm not sure.
But have a look at Msdn.
And a sample: http://www.netomatix.com/ImpersonateUser.aspx
-
Jun 6th, 2007, 09:35 AM
#12
Thread Starter
New Member
Re: How to access an Service on a Remote PC (Start/Stop it!!!)
Hi steve_rm
Hi sapator
I have already seen the link to Microsoft descriping the application to start and stop a service. But this application runs on the same PC as the service. I have 2 PC one with the service and one with the VB application. (The 2 PC are on the same network and there are access between them)
I have made a temperaly solution to the problem, by going trough SQL. From SQL it is possible to run DOC commands. (permission needs to be switched on!) then executing this SELECT statement on the Database on the remote PC from within VB:
Code:
SELECT EXEC xxxxxxxxx (somthing like this!!)
But I do not like the solution, and I need to remeber to switch off the permission to run DOS commands from withing the SQL studio, whne finished.
Peter
-
Jun 6th, 2007, 02:00 PM
#13
Frenzied Member
Re: How to access an Service on a Remote PC (Start/Stop it!!!)
Hello,
http://www.codeguru.com/cpp/w-p/syst...cle.php/c5787/
Here is another link, not sure if it is what you need.
It would be good to find a solid solution for this solution, instead of your work around. I am sure that many programmers must have done something like this before.
Steve
-
Mar 13th, 2008, 03:55 PM
#14
Lively Member
Re: How to access an Service on a Remote PC (Start/Stop it!!!)
I know this is almost a year later but did anyone get a solution to this?
I *had* working code that would start/stop remotely. I was using impersonation for the starts and stops, and un-doing it right after. I was however not using impersonation to check the remote service's status. Now, this machine has been joined on a domain and they claim that it has no sec policies I should worry about because it's in a loopback OU on the domain.
both machines have a matching windows account names, are on the same subnet, and are part of the administrators groups.
here's my error:
"Error System.InvalidOperationException: Cannot open Service Control Manager on computer '10.103.2.43'. This operation might require other privileges.
---> System.ComponentModel.Win32Exception: Access is denied --- End of inner exception stack trace --- at System.ServiceProcess.ServiceController.GetDataBaseHandleWithAccess(String machineName, Int32 serviceControlManaqerAccess) at System.ServiceProcess.ServiceController.GetDataBaseHandleWithConnectAccess() at System.ServiceProcess.ServiceController.GenerateNames() at System.ServiceProcess.ServiceController.get_ServiceName() at System.ServiceProcess.ServiceController.GenerateStatus() at System.ServiceProcess.ServiceController.get_Status() at appMonitor.ctrlApplication.pCheckProcesses(String appName)"
So, can someone recommend another thing for me to check to make this code work again? I have tried forcing an impersonate right before the "pcheckprocesses" but I get the same exact results.
Somewhere some permission has gotten turned off that at one time did work. Thanks to anyone that can help lead me in the right direction.
-
Apr 11th, 2008, 12:41 PM
#15
New Member
Re: How to access an Service on a Remote PC (Start/Stop it!!!)
I've been searching and searching and searching for code on how to change the StartMode of a service (IE Enable and Disable) on remote computers, but can't seem to locate that functionality.
I know it can be done, because I can do it in VBScript. In VBScript, I use...
Code:
'Restart the Automatic Updates service (Prompt to Enable service if it is disabled).
Set oService = GetObject("WinMgmts:{impersonationLevel=impersonate}!//" & strPCName & "/root/cimv2:Win32_Service.Name='wuauserv'")
If Err = 462 Then
MsgBox "Computer " & strPCName & " does not exist.", 16, "Error"
Else
If oService.StartMode = "Disabled" Then
Auto = MsgBox("The Automatic Updates service on " & strPCName & " is set to Disabled." & vbCRLF & "Do you want to set it to Automatic?", 36, "Automatic Updates Service")
If Auto = vbYes Then
oService.ChangeStartMode("Automatic")
Else
If oService.StartMode = "Manual" Then
Auto = MsgBox("The Automatic Updates service on " & strPCName & " is set to Manual." & vbCRLF & "Do you want to set it to Automatic?", 36, "Automatic Updates Service")
If Auto = vbYes Then
oService.ChangeStartMode("Automatic")
End If
oService.StopService
End If
'Wait until the service stops
Do
Set oService = GetObject("WinMgmts:{impersonationLevel=impersonate}!//" & strPCName & "/root/cimv2:Win32_Service.Name='wuauserv'")
Loop Until oService.State = "Stopped"
'Start the Automatic Updates service
Set oService = GetObject("WinMgmts:{impersonationLevel=impersonate}!//" & strPCName & "/root/cimv2:Win32_Service.Name='wuauserv'")
oService.StartService
'Wait until the service starts
Do
Set oService = GetObject("WinMgmts:{impersonationLevel=impersonate}!//" & strPCName & "/root/cimv2:Win32_Service.Name='wuauserv'")
Loop Until oService.State = "Running"
End If
End If
But I need to be able to do the same thing in my GUI vb.net app, and prefer not to call an external VBscript. Anyone have any suggestions?
Thanks in advance.
Dave Wright
-
Apr 11th, 2008, 04:10 PM
#16
New Member
Re: How to access an Service on a Remote PC (Start/Stop it!!!)
Never mind... I answered my own questions after more research.
Here is the code that worked...
Code:
Private Sub cmdEnableService_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEnableService.Click
Try
Dim SvcMgr As New Management.ManagementObject(Nothing, New Management.ManagementPath("\\" & strComputer & "\root\CIMV2:Win32_Service.Name=""" & ctlServiceController.ServiceName & """"), Nothing)
Dim inParams As Management.ManagementBaseObject = Nothing
inParams = SvcMgr.GetMethodParameters("ChangeStartMode")
inParams("StartMode") = "Automatic"
' inParams("StartMode") = "Disabled" <--- If you want to DISABLE a service instead.
Dim outParams As Management.ManagementBaseObject = SvcMgr.InvokeMethod("ChangeStartMode", inParams, Nothing)
Catch ex As Exception
lblStatusBarLabel.Text = "Error: Unable to enable " & ctlServiceController.DisplayName.ToString & " on " & strComputer
lblStatusBarLabel.ForeColor = Color.Red
End Try
End Sub
Hope this helps... I have tested it and it works great on local and remote machines once authenticated. I have not tried it on a workgroup computer, but I suspect it won't work. If anyone ever figures out how to query a workgroup PC, I'd love to hear all about how.
Thanks,
Dave Wright
-
Apr 11th, 2008, 04:58 PM
#17
Re: How to access an Service on a Remote PC (Start/Stop it!!!)
Yes old post indeed
I have created an old 2003 that user remote and local services.
I have tested the application on a network that i have administrative privileges and it works like a charm.
I used ServiceController with servicename and computername.
Never tried on workgroup.
Also if some services fail to start or stop it's not the applications fault (p.e. you cannot (and must not) stop the VGA Display controller service )
Anyway i just show your code and us much as i like WMI i think it's unnecessary complication here.
(tested on XP not Vista)
P.S. I saw (may be wrong) that u use IP's for passing the commands. I find it working better if you resolve the IP's to actually computer names and pass this value to the servicecontroller.
-
Apr 14th, 2008, 09:04 AM
#18
New Member
Re: How to access an Service on a Remote PC (Start/Stop it!!!)
I am using the service controller to start and stop services, but see no way to enable or disable or otherwise change the startup type for services without using WMI. Unless I am missing something.... If I am, please enlighten me.
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
|