How to run services of one pc from another pc

0

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

try
{
    var serviceName = "MyService";
    var ip = "10.10.11.16";
    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("\\\\10.10.11.16\\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";
            }
            else
            {
                service.InvokeMethod("StartService", null);
                BtnStartStop.Content = "Stop";
                LblService.Content = serviceName;
                LblServiceStatus.Content = "Running";
            }
        }
    }
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

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

https://docs.microsoft.com/en-us/windows/win32/wmisdk/connecting-to-wmi-remotely-starting-with-vista

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:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\WBEM\CIMOM\AllowAnonymousCallback

           Data type

           REG\_DWORD

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

I still get the same error.

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

1 Answer

0

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