Results 1 to 18 of 18

Thread: How to access an Service on a Remote PC (Start/Stop it!!!)

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2007
    Posts
    6

    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!!

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

    Re: How to access an Service on a Remote PC

    Quote 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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2007
    Posts
    6

    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

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

    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:
    1. Try
    2.     'Put your old code here.
    3. Catch ex As Exception
    4.     MessageBox.Show(ex.ToString())
    5. End Try
    You can do more than that too but it's a good place to start.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2007
    Posts
    6

    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

  6. #6
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    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

  7. #7
    Fanatic Member
    Join Date
    Apr 2006
    Posts
    746

    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

  8. #8

    Thread Starter
    New Member
    Join Date
    May 2007
    Posts
    6

    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

  9. #9

    Thread Starter
    New Member
    Join Date
    May 2007
    Posts
    6

    Re: How to access an Service on a Remote PC

    Are there realy nobody that konws how to do this?
    Peter

  10. #10
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    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.
    steve

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

    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

  12. #12

    Thread Starter
    New Member
    Join Date
    May 2007
    Posts
    6

    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

  13. #13
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    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
    steve

  14. #14
    Lively Member
    Join Date
    Mar 2008
    Location
    Charlotte, NC USA
    Posts
    70

    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.

  15. #15
    New Member
    Join Date
    Apr 2008
    Posts
    3

    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

  16. #16
    New Member
    Join Date
    Apr 2008
    Posts
    3

    Cool 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

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

    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.

  18. #18
    New Member
    Join Date
    Apr 2008
    Posts
    3

    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
  •  



Click Here to Expand Forum to Full Width