How to run services of one pc from another pc


I have a code that tries to access the services of another computer.

    var serviceName = "MyService";
    var ip = "";
    var username = "SomeUser";
    var password = "APassword";
    var connectoptions = new ConnectionOptions();
    connectoptions.Impersonation = ImpersonationLevel.Impersonate;
    connectoptions.Authentication = AuthenticationLevel.Packet;
    connectoptions.EnablePrivileges = true;
    connectoptions.Username = username;
    connectoptions.Password = password;
    var scope = new ManagementScope("\\\\\\root\\cimv2");
    scope.Options = connectoptions;

    var query = new SelectQuery("select * from Win32_Service where name = '" + serviceName + "'");
    using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query))
        var collection = searcher.Get();
        foreach (ManagementObject service in collection.OfType<ManagementObject>())
            if (service["started"].Equals(true))
                service.InvokeMethod("StopService", null);
                BtnStartStop.Content = "Stop";
                LblService.Content = serviceName;
                LblServiceStatus.Content = "Stopped";
                service.InvokeMethod("StartService", null);
                BtnStartStop.Content = "Stop";
                LblService.Content = serviceName;
                LblServiceStatus.Content = "Running";
catch (Exception ex)

Will this work on Server and client only? Won't this work on regular pc to another regular pc? Each time I run this when I get to the part of:

var collection = searcher.Get();

I get an error of

Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

Do you have an idea on to make this work? Thank you.

STEPS DONE SO FAR Followed the instruction on

typed in the cmd with admin privilege

netsh advfirewall firewall set rule group="windows management instrumentation (wmi)" new enable=yes

I even turned off the firewall just to be sure.

edited the registry of the pc I am connecting to this:


           Data type


As for the antivirus, the pc I am connecting to does not have any anti virus.

I still get the same error.

asked on Stack Overflow Aug 22, 2019 by Ibanez1408 • edited Aug 23, 2019 by Ibanez1408

1 Answer


As @colosso pointed out, you are receiving that error message because you do not have permission on the remote host to connect to the WMI service.

You should follow the instructions here to ensure the remote host is configured to allow your connection.

answered on Stack Overflow Aug 22, 2019 by Aleks

User contributions licensed under CC BY-SA 3.0